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[:] 也是可以的!
Python笔记 #02# Inner workings of lists的更多相关文章
- 我的Python笔记02
声明:本文整理借鉴金角大王的Python之路,Day2 - Python基础2,仅供本人学习使用!!! 本节内容 列表.元组操作 字符串操作 字典操作 集合操作 文件操作 字符编码与转码 1. 列表. ...
- python笔记02:列表与元素
本章将引入一个新的概念:数据结构.数据结构是通过某种方式(例如对元素进行编号)组织在一起的数据元素的集合.这些数据元素可以是数字或者字符,甚至可以是其他数据结构.在python中,最基本的数据结构是序 ...
- python笔记02
day02笔记记录 一.今日摘要 循环.字符串格式化.运算符.编码.博客. 二.内容回顾 (一)计算机基础 计算机由硬件和软件组成.传统计算机的硬件一般有输入单元.输出单元,算数逻辑单元.控制单元及记 ...
- python笔记-02
Python基础知识 —————————————— A,B,先把A乘以3,然后加上B,最后在加上列表A A = [1, 2, 3, 4, 5, 6] 赋值 B = [1, 2, 3] 变量 定义一个变 ...
- Python学习02 列表 List
Python学习02 列表 List Python列表 List Python中的列表(List)用逗号分隔,方括号包围(comma-separated values (items) between ...
- python笔记 - day6
python笔记 - day6 参考: http://www.cnblogs.com/wupeiqi/articles/5501365.html 大纲: 利用递归,实现阶乘: Python反射 pyt ...
- guxh的python笔记一:数据类型
1,基本概念 1.1,数据类型 基本数据类型:字符串,数字,布尔等 引用数据类型:相对不可变(元组),可变(列表,字典,集合等) 基本数据类型存放实际值,引用数据类型存放对象的地址(即引用) ==:判 ...
- python笔记-1(import导入、time/datetime/random/os/sys模块)
python笔记-6(import导入.time/datetime/random/os/sys模块) 一.了解模块导入的基本知识 此部分此处不展开细说import导入,仅写几个点目前的认知即可.其 ...
- 机器学习实战(Machine Learning in Action)学习笔记————02.k-邻近算法(KNN)
机器学习实战(Machine Learning in Action)学习笔记————02.k-邻近算法(KNN) 关键字:邻近算法(kNN: k Nearest Neighbors).python.源 ...
随机推荐
- pushViewController自定义动画http://blog.csdn.net/ralbatr/article/details/22039233
本文转载至 http://blog.csdn.net/ralbatr/article/details/22039233 实现的主要代码如下: CATransition *transition = ...
- JSP内置对象——request 及其响应get和post请求的实例
request对象客户端的请求信息被封装在request对象中,通过它才能了解到客户的需求,然后做出响应.它是HttpServletRequest类的实例.request对象具有请求域,即完成客户端的 ...
- python反序列化研究学习
零.补充: 补充于2018-02-08,之前研究时候有一个疑惑,python的序列化成二进制,打web服务怎么传这个二进制对象呢,今天请教了身边大神(传说的九零后黑客代表),可以使用base64传输. ...
- 此类目的是防治序列化Json字符串时的循环引用问题-------最好解决方案
http://james.newtonking.com/json/help/index.html using Newtonsoft.Json;using System;using System.Col ...
- json序列化懒加载问题
如果框架使用了json序列化对象,当配置了hibernate懒加载时,可能会抛出异常,或者出现N+1的问题,或者出现无限循环的问题.网上很多解决方案, 基本是这些:@JsonIgnore忽略可能出问题 ...
- HUD2647 Reward_反向建图拓扑排序
HDU2647 Reward 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 题意:老板要发奖金了,有n个人,给你m对数,类似a b,这样的一对 ...
- Linux常用软件(以及特殊命令)清单(ubuntu)
LibreOffice 解压缩命令 unar .tar 创建新文档命令:touch.vi/vim 浏览器:google chrome.firefox
- 修改计算机名后SQLServer无法使用windows账号登录
USE master GO EXEC sp_configure'allow updates',1 RECONFIGURE WITH OVERRIDE -- 设置两个变量 DEC ...
- vue中获取客户端IP地址(不需要额外引入三方文件)
之前看了几种方法 ,都是引入腾讯,新浪,搜狐等的三方js文件来查询IP地址,但是我自己测试的时候IP地址不准确,所以就找了找,发现了这个方法,准确的获取到了IP地址和cmd的ipconfig获取到的I ...
- pycharm自定义代码片段
pycharm自定义代码片段 目录 (一)通用阶段 0 .新建.py文件模板:2 0 .pycharm中添加自定义代码片段:一图全知道:3 1 .定义类:classin 描述 ...