Python笔记 #02# Inner workings of lists
源:DataCamp
datacamp 的 DAILY PRACTICE + 日常收集。
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[:] 也是可以的!
最新文章
- oracle导出一条二进制数据(二进制,long只能通过dmp导出)
- MAC下彻底解决mysql无法插入和显示中文
- Nginx的启动脚本
- 使用MySQL处理百万级以上数据时,不得不知道的几个常识
- aspx.cs上传文件
- asp.net用Zxing库实现条形码输出
- 微信小程序实例教程(二)
- JavaScript中两个对象数组 属性undefined
- PyCharm中设置字体大小
- day18 python之re模块与正则表达式
- rem 自适应适配方法
- SQLFullbackup
- spring4笔记----spring4设值注入
- JSP学习记录
- EOS 权限
- 无网情况下linux安装django
- Android 开发工具推荐
- VC异常.简单抛,简单捕获
- [HEOI2016/TJOI2016]字符串(后缀数组+二分+主席树/后缀自动机+倍增+线段树合并)
- Linq to Sql并发冲突及处理策略
热门文章
- 五款超实用的开源 SVG 工具
- std::condition_variable
- poj_2486 动态规划
- 使用async/await——Nodejs+ExpressJs+Babel
- HDU1536 S-Nim
- LAMP集群项目四 安装apache、php及其插件
- 参数估计(1):从最小二乘到最小b乘
- 170308、oracle查看被锁的表和解锁
- bootstrap datetimepicker 日期插件超详细使用方法
- 关于ios7 UINavigationController.interactivePopGestureRecognizer手势集成