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 intbool 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"]
del(x[1])
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!
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的更多相关文章

  1. 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 ...

  2. 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- ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. Intermediate Python for Data Science learning 2 - Histograms

    Histograms from:https://campus.datacamp.com/courses/intermediate-python-for-data-science/matplotlib? ...

  8. 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 ...

  9. Intermediate Python for Data Science learning 3 - Customization

    Customization from:https://campus.datacamp.com/courses/intermediate-python-for-data-science/matplotl ...

随机推荐

  1. Apache 的mod_auth_cas模块的介绍和使用

    apache的mod_auth_cas模块是一个集成到apache中的cas客户端,一般是配合Apache的反向代理来使用,对某个url的请求先经过apache,apache会判断是否经过cas认证, ...

  2. Java虚拟机九 java.lang.String在虚拟机中的实现

    在Java中,Java的设计者对String对象进行了大量的优化,主要有三个特点: 1.不变性: 不变性是指String对象一旦生成,则不能再对它进行改变.String的这个特点可以泛化成不变(imm ...

  3. Centos6.5 虚拟机Mongodb创建副本集

    简单副本集的搭建 官方demo的最小化的副本集为Three Member Sets,一个primary和两个secondary.我们先就搭建一个这样的测试环境. 首先建立三个数据目录和日志目录: cd ...

  4. 转sklearn保存模型

    训练好了一个Model 以后总需要保存和再次预测, 所以保存和读取我们的sklearn model也是同样重要的一步. 比如,我们根据房源样本数据训练了一下房价模型,当用户输入自己的房子后,我们就需要 ...

  5. CAT偶现NPE的问题

    1.背景 我们公司的调用链系统是基于大众点评的CAT客户端改造的,服务端完全有自己设计开发的.在是用CAT客户端收集dubbo调用信息的时候,我们发现了一个CAT偶现NPE的bug,该bug隐藏的很深 ...

  6. CSS在网页中应用的方式_嵌入式

    内联式样式表:直接写在现有的标记中,比如: 复制代码 代码如下: <p style="font-size:24px;">www.phpstudy.net</p&g ...

  7. FTP的两种主动模式和被动模式

    参考:https://blog.csdn.net/xqhrs232/article/details/54633006 https://blog.csdn.net/yuanhangq220/articl ...

  8. TOP100summit 2017:【案例分享】魅族持续交付平台建设实践

    本篇文章内容来自第10期魅族开放日魅族运维架构师林钟洪的现场分享.编辑:Cynthia 一.自动化建设历程1.1 魅族互联网发展的时间线 2003-2008年被称之为“互联网1.0时代”.2003年, ...

  9. Solr学习笔记之4、Solr配置文件简介

    Solr学习笔记之4.Solr配置文件简介 摘自<Solr in Action>. 1. solr.xml – Defines one or more cores per Solr ser ...

  10. GCD之各种派发

    dispatch_apply的用法 并行模拟for循环,将指定的代码循环10次,一般会把这些代码附加到一个queue上,然后在 dispatch_apply里并行 dispatch_queue_t q ...