首页 技术 正文
技术 2022年11月10日
0 收藏 914 点赞 4,956 浏览 3035 个字

源:DataCamp

datacamp 的 DAILY PRACTICE  + 日常收集。

List of lists

Subset and conquer

Slicing and dicing

List Manipulation

List of lists

As a data scientist, you’ll often be dealing with a lot of data, and it will make sense to group some of this data.

Instead of creating a flat list containing strings and floats, representing the names and areas of the rooms in your house, you can create a list of lists. The script on the right can already give you an idea.

Don’t get confused here: "hallway" is a string, while hall is a variable that represents the float 11.25 you specified earlier.

# area variables (in square meters)
hall = 11.25
kit = 18.0
liv = 20.0
bed = 10.75
bath = 9.50# house information as list of lists
house = [["hallway", hall],
["kitchen", kit],
["living room", liv],
["bedroom", bed],
["bathroom", bath]]# Print out house
print(house)# Print out the type of house
print(type(house))

Subset and conquer

一个元素毫无疑问也是一个子集,因此不论是取一段或者随机访问一个元素都叫做 subsetting

# Create the areas list
areas = ["hallway", 11.25, "kitchen", 18.0, "living room", 20.0, "bedroom", 10.75, "bathroom", 9.50]# Print out second element from areas
print(areas[1])# Print out last element from areas
print(areas[-1])# Print out the area of the living room
print(areas[5])

#Subset and calculate

# Create the areas list
areas = ["hallway", 11.25, "kitchen", 18.0, "living room", 20.0, "bedroom", 10.75, "bathroom", 9.50]# Sum of kitchen and bedroom area: eat_sleep_area
eat_sleep_area = areas[3] + areas[-3]# Print the variable eat_sleep_area
print(eat_sleep_area)

Slicing and dicing

Python笔记 #02# Inner workings of lists

Selecting single values from a list is just one part of the story. It’s also possible to slice your list, which means selecting multiple elements from your list. Use the following syntax:

my_list[start:end]

The start index will be included, while the end index is not.

# Create the areas list
areas = ["hallway", 11.25, "kitchen", 18.0, "living room", 20.0, "bedroom", 10.75, "bathroom", 9.50]# Use slicing to create downstairs
downstairs = areas[0:6]# Use slicing to create upstairs
upstairs = areas[6:10]# Print out downstairs and upstairs
print(downstairs)
print(upstairs)

However, it’s also possible not to specify these indexes. If you don’t specify the begin index, Python figures out that you want to start your slice at the beginning of your list. If you don’t specify the end index, the slice will go all the way to the last element of your list.

# Create the areas list
areas = ["hallway", 11.25, "kitchen", 18.0, "living room", 20.0, "bedroom", 10.75, "bathroom", 9.50]# Alternative slicing to create downstairs
downstairs = areas[:6]# Alternative slicing to create upstairs
upstairs = areas[6:]

List Manipulation

1、反向选就不用考虑 0 了,数到几就是几

2、如下操作也可以增加元素

In [3]: x
Out[3]: ['a', 'b', 's', 't']
In [4]: x[2:] = ["s", "t", "hi"]
In [5]: x
Out[5]: ['a', 'b', 's', 't', 'hi']

Extend a list

# Create the areas list and make some changes
areas = ["hallway", 11.25, "kitchen", 18.0, "chill zone", 20.0,
"bedroom", 10.75, "bathroom", 10.50]# Add poolhouse data to areas, new list is areas_1
areas_1 = areas + ["poolhouse", 24.5]# Add garage data to areas_1, new list is areas_2
areas_2 = areas_1 + ["garage", 15.45]

Delete list elements

del(areas[-4:-2])

Inner workings of lists

Change the second command, that creates the variable areas_copy, such that areas_copy is an explicit copy of areas

# Create list areas
areas = [11.25, 18.0, 20.0, 10.75, 9.50]# Create areas_copy
# areas_copy = areas
areas_copy = list(areas)
# Change areas_copy
areas_copy[0] = 5.0# Print areas
print(areas)

areas_copy = areas[:] 也是可以的!

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,493
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,133
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,297