源: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

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[:] 也是可以的!

最新文章

  1. oracle导出一条二进制数据(二进制,long只能通过dmp导出)
  2. MAC下彻底解决mysql无法插入和显示中文
  3. Nginx的启动脚本
  4. 使用MySQL处理百万级以上数据时,不得不知道的几个常识
  5. aspx.cs上传文件
  6. asp.net用Zxing库实现条形码输出
  7. 微信小程序实例教程(二)
  8. JavaScript中两个对象数组 属性undefined
  9. PyCharm中设置字体大小
  10. day18 python之re模块与正则表达式
  11. rem 自适应适配方法
  12. SQLFullbackup
  13. spring4笔记----spring4设值注入
  14. JSP学习记录
  15. EOS 权限
  16. 无网情况下linux安装django
  17. Android 开发工具推荐
  18. VC异常.简单抛,简单捕获
  19. [HEOI2016/TJOI2016]字符串(后缀数组+二分+主席树/后缀自动机+倍增+线段树合并)
  20. Linq to Sql并发冲突及处理策略

热门文章

  1. 五款超实用的开源 SVG 工具
  2. std::condition_variable
  3. poj_2486 动态规划
  4. 使用async/await——Nodejs+ExpressJs+Babel
  5. HDU1536 S-Nim
  6. LAMP集群项目四 安装apache、php及其插件
  7. 参数估计(1):从最小二乘到最小b乘
  8. 170308、oracle查看被锁的表和解锁
  9. bootstrap datetimepicker 日期插件超详细使用方法
  10. 关于ios7 UINavigationController.interactivePopGestureRecognizer手势集成