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

Python笔记 #02# Inner workings of lists的更多相关文章

  1. 我的Python笔记02

    声明:本文整理借鉴金角大王的Python之路,Day2 - Python基础2,仅供本人学习使用!!! 本节内容 列表.元组操作 字符串操作 字典操作 集合操作 文件操作 字符编码与转码 1. 列表. ...

  2. python笔记02:列表与元素

    本章将引入一个新的概念:数据结构.数据结构是通过某种方式(例如对元素进行编号)组织在一起的数据元素的集合.这些数据元素可以是数字或者字符,甚至可以是其他数据结构.在python中,最基本的数据结构是序 ...

  3. python笔记02

    day02笔记记录 一.今日摘要 循环.字符串格式化.运算符.编码.博客. 二.内容回顾 (一)计算机基础 计算机由硬件和软件组成.传统计算机的硬件一般有输入单元.输出单元,算数逻辑单元.控制单元及记 ...

  4. python笔记-02

    Python基础知识 —————————————— A,B,先把A乘以3,然后加上B,最后在加上列表A A = [1, 2, 3, 4, 5, 6] 赋值 B = [1, 2, 3] 变量 定义一个变 ...

  5. Python学习02 列表 List

    Python学习02 列表 List Python列表 List Python中的列表(List)用逗号分隔,方括号包围(comma-separated values (items) between ...

  6. python笔记 - day6

    python笔记 - day6 参考: http://www.cnblogs.com/wupeiqi/articles/5501365.html 大纲: 利用递归,实现阶乘: Python反射 pyt ...

  7. guxh的python笔记一:数据类型

    1,基本概念 1.1,数据类型 基本数据类型:字符串,数字,布尔等 引用数据类型:相对不可变(元组),可变(列表,字典,集合等) 基本数据类型存放实际值,引用数据类型存放对象的地址(即引用) ==:判 ...

  8. python笔记-1(import导入、time/datetime/random/os/sys模块)

    python笔记-6(import导入.time/datetime/random/os/sys模块)   一.了解模块导入的基本知识 此部分此处不展开细说import导入,仅写几个点目前的认知即可.其 ...

  9. 机器学习实战(Machine Learning in Action)学习笔记————02.k-邻近算法(KNN)

    机器学习实战(Machine Learning in Action)学习笔记————02.k-邻近算法(KNN) 关键字:邻近算法(kNN: k Nearest Neighbors).python.源 ...

随机推荐

  1. poj_1258 prim最小生成树

    题目大意 给定N个点,以及每两个点之间的路径长度,求出一个连接这N个点的方案,使得连接这N个点的总长度最短,求出该总长度. 题目分析 求最小生成树MST的模板题,直接使用prim算法进行求解. 实现( ...

  2. [转发]CentOS7安装MySQL

    在CentOS中默认安装有MariaDB,这个是MySQL的分支,但为了需要,还是要在系统中安装MySQL,而且安装完成之后可以直接覆盖掉MariaDB. 1 下载并安装MySQL官方的 Yum Re ...

  3. Array.prototype.filter(Boolean)

    ES5 中的数组有这个方法:Array.prototype.filter ,具体使用参考MDN,这里讲一个特殊应用: 回顾下语法: new_array = arr.filter(callback[, ...

  4. js 常用 DOM 元素宽高

    提示:document.documentElement 和 document.getElementsByTagName('html')[0] 是一样的: 1.视口大小(不包括滚动条,视口字面理解当然是 ...

  5. 深入HQL学习以及HQL和SQL的区别

    HQL(Hibernate Query Language) 是面向对象的查询语言, 它和 SQL 查询语言有些相似. 在 Hibernate 提供的各种检索方式中, HQL 是使用最广的一种检索方式. ...

  6. 20165330 2017-2018-2 《Java程序设计》第2周学习总结

    课本知识总结 第二章 基本数据类型与数组 标识符:标识类名.变量名.方法名.类型名.数组名及文件名的有效字符序列. 标识符的第一个字符不能是数字字符,且字母区分大小写. Java语言使用Unicode ...

  7. 微软构建高效DevOps团队培训总结

    9.21和9.22这两天参加了微软DevOps的培训,主要是围绕TFS2015的不少新功能来讲的,相比较之前我们一直使用TFS2013来管理团队,确实强大了不少,也更加实用了. 首先,什么是DevOp ...

  8. King's Game---hdu5643(约瑟夫环)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5643    约瑟夫环问题的原来描述为,设有编号为1,2,……,n的n(n>0)个人围成一个圈,从 ...

  9. HTTP API 设计指南(转)

    add by zhj (2014-12-16): 今天才知道,原画HeroKu是国外一个很有名的PaaS提供商,公司很可能会将app迁移到他们那里 英文原文: HTTP API Design Guid ...

  10. SpringBoot与Mybatis整合实例详解

    介绍 从Spring Boot项目名称中的Boot可以看出来,SpringBoot的作用在于创建和启动新的基于Spring框架的项目,它的目的是帮助开发人员很容易的创建出独立运行的产品和产品级别的基于 ...