Intro to Python for Data Science Learning 2 - List
List
from:https://campus.datacamp.com/courses/intro-to-python-for-data-science/chapter-2-python-lists?ex=2
-----------------1-------------------------------------
- Create a list
As opposed to int, bool etc., a list is a compound data type; you can group values together:
a = "is"
b = "nice"
my_list = ["my", "list", a, b]
--------------2----------------------------------------
Create list with different types
A list can contain any Python type. Although it's not really common, a list can also contain a mix of Python types including strings, floats, booleans, etc.
# area variables (in square meters)
hall = 11.25
kit = 18.0
liv = 20.0
bed = 10.75
bath = 9.50
# Adapt list areas
areas = ["hallway",hall, "kitchen",kit, "living room", liv, "bedroom",bed, "bathroom", bath]
# Print areas
print(areas)
---------------3-------------------------------------
- Select the valid list
my_list = [el1, el2, el3]
both of them are correct:A. [1, 3, 4, 2] B. [[1, 2, 3], [4, 5, 7]] C. [1 + 2, "a" * 5, 3]
Subsetting lists
Subset and conquer
x = ["a", "b", "c", "d"]
x[1]
x[-3] # same result!
Subset and calculate
x = ["a", "b", "c", "d"]
print(x[1] + x[3])
Slicing and dicing
x = ["a", "b", "c", "d"]
x[1:3]
The elements with index 1 and 2 are included, while the element with index 3 is not.
Slicing and dicing (2)
x = ["a", "b", "c", "d"]
x[:2]
x[2:]
x[:]
Subsetting lists of lists
x = [["a", "b", "c"],
["d", "e", "f"],
["g", "h", "i"]]
x[2][0]
x[2][:2]
List Manipulation
--------------------1-----------------------
Replace list elements
x = ["a", "b", "c", "d"]
x[1] = "r"
x[2:] = ["s", "t"]
-------------------2------------
Extend a list
x = ["a", "b", "c", "d"]
y = x + ["e", "f"]
-------------------3------------
Delete list elements
x = ["a", "b", "c", "d"]Pay attention here: as soon as you remove an element from a list, the indexes of the elements that come after the deleted element all change!
del(x[1])
The;sign is used to place commands on the same line. The following two code chunks are equivalent:
# Same line
command1; command2
# Separate lines
command1
command2
So if delete two items, pay more attention here. better to use like this del(areas[-4:-2]), instead of "del(areas[10]); del(areas[11])","del(areas[10:11])" "del(areas[-3]); del(areas[-4])",
----------------------------------------------4--------------------------------------------------------------
Inner workings of lists
if use "areas_copy = areas", then they are the same thing, when value in one of them changed, both of them will be changed.
if use "areas_copy = areas[:]", then they are not the same thing, when value in one of them changed, the other one will Not be changed.
Intro to Python for Data Science Learning 2 - List的更多相关文章
- Intro to Python for Data Science Learning 8 - NumPy: Basic Statistics
NumPy: Basic Statistics from:https://campus.datacamp.com/courses/intro-to-python-for-data-science/ch ...
- Intro to Python for Data Science Learning 7 - 2D NumPy Arrays
2D NumPy Arrays from:https://campus.datacamp.com/courses/intro-to-python-for-data-science/chapter-4- ...
- Intro to Python for Data Science Learning 5 - Packages
Packages From:https://campus.datacamp.com/courses/intro-to-python-for-data-science/chapter-3-functio ...
- Intro to Python for Data Science Learning 6 - NumPy
NumPy From:https://campus.datacamp.com/courses/intro-to-python-for-data-science/chapter-4-numpy?ex=1 ...
- Intro to Python for Data Science Learning 4 - Methods
Methods From:https://campus.datacamp.com/courses/intro-to-python-for-data-science/chapter-3-function ...
- Intro to Python for Data Science Learning 3 - functions
Functions from:https://campus.datacamp.com/courses/intro-to-python-for-data-science/chapter-3-functi ...
- Intermediate Python for Data Science learning 2 - Histograms
Histograms from:https://campus.datacamp.com/courses/intermediate-python-for-data-science/matplotlib? ...
- Intermediate Python for Data Science learning 1 - Basic plots with matplotlib
Basic plots with matplotlib from:https://campus.datacamp.com/courses/intermediate-python-for-data-sc ...
- Intermediate Python for Data Science learning 3 - Customization
Customization from:https://campus.datacamp.com/courses/intermediate-python-for-data-science/matplotl ...
随机推荐
- C# 二维码 ThoughtWorks.QRCode.dll
ThoughtWorks.QRCode.dll: 1.ThoughtWorks.QRCode.dll 2.通过 NuGet 添加 后台代码: using System; using System.Dr ...
- 使用disavled属性锁定input内容不可以修改后,打印获取不到对应的值
当我们需要锁定input内容不让修改时,可以使用disabled="disabled"和readonly="readonly", 官方的解释是:disabled ...
- Linux shell一行流编程实践
Linux下很多命令用起来真相当方便,尤其是进行批处理操作时.(话说感觉这种程序也不复杂,windows咋一直不搞一个好用的shell呢) 这里列出一些实际shell操作的应用场景,具体命令的用法与解 ...
- ArchLinux 添加国内镜像源
$ vim /etc/pacman.d/mirrorlist # 在最前面添加一行,这样就成功添加了网易的源: Server = http://mirrors.163.com/archlinux/$r ...
- 小程序中的block
<block/> 并不是一个组件,它仅仅是一个包装元素,不会在页面中做任何渲染,只接受控制属性. 因为 wx:if 是一个控制属性,需要将它添加到一个标签上.如果要一次性判断多个组件标签, ...
- HDU_6043_KazaQ's Socks
KazaQ's Socks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)T ...
- ElasticSearch报 EsThreadPoolExecutor[search, queue capacity = 1000, org.elasticsearch.common.util.concurrent.EsThreadPoolExecutor@c0efba
ElasticSearch报以下错误的解决办法: "type": "es_rejected_execution_exception", "reason ...
- 不移除通知的话,出现的bug
没销毁的私聊控制器仍然监听到了通知,发送表情
- jquery ajax contentType设置
默认get方法没有contentType,post方法的contentType为:application/x-www-form-urlencoded; charset=UTF-8 (1) 设置成app ...
- 15款Django开发常用软件包(转)
原文:http://www.iteye.com/news/28697 Django是一款高级的Python Web框架,可以帮助开发者快速创建web应用.我们这里整理了15款Django开发中常用的软 ...