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.源 ...
随机推荐
- 自记(项目npm)
cnpm install pug pug-cli vuex node-sass sass-loader vue-beauty axios 在main.js里面使用vue-beauty: import ...
- 给vmware虚拟机设置Ip,使用xshell远程连接Centos
参考下面两位的分享才弄好,发表之前先对原作者表示感谢! 给Centos配置网络以及使用xshell远程连接Centos http://www.cnblogs.com/fuly550871915/p/4 ...
- AI画圆角矩形
如何画圆角矩形:设置矩形圆角大小 第一种方法:点击圆角矩形在画布上点一下; [caption id="attachment_878" align="alignnone&q ...
- python--base64
import base64import os # base64,参数为文件路径名def file_base64(filepath): if os.path.isfile(filepath): with ...
- Hibernate的调用数据库的存储过程
Hibernate并没有给出直接调用数据库的存储过程的API,所以咋们就要通过调用原生的的connection对象来实现对存储过程的条用 Hibernate调用存储过程的步骤: 1:获得原生conne ...
- c#修改cpu主频
并不是真正能修改硬件,只是一个数据,能骗过部分程序检测,如英雄联盟必须达到3.0的主频才能使用录像功能,通过修改可以达到要求. 下面是代码: public enum RegValueKind { // ...
- SpringBoot 与缓存
1. JSR107 Java Caching 定义了5个核心接口: CachingProvider:定义了创建,配置,获取,管理和控制多个CacheManager; CacheManager:定义了创 ...
- Clairewd’s message--hdu4300(Next数组的运用)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4300 题意就是给你26的字母的加密方式,然后又给了一个串s1是包含加密后的和没有加密的但是没有加密的可 ...
- Design Patterns Example Code (in C++)
Overview Design patterns are ways to reuse design solutions that other software developers have crea ...
- 如何在python项目中写出像Django中一样功能的settings
一 核心文件目录结构 二 实现代码 resdme: 在实现此功能主要用到的知识点及模块: 1.反射 3.内置方法dir # 全局配置 NAME = 'root' # 用户配置 NAME = 'pe ...