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. 【BZOJ5133】[CodePlus2017年12月]白金元首与独舞 矩阵树定理

    [BZOJ5133][CodePlus2017年12月]白金元首与独舞 题面:www.lydsy.com/JudgeOnline/upload/201712/div1.pdf 题解:由于k很小,考虑用 ...

  2. MUI---上传头像功能实现

    这里使用MUI上传头像的功能是结合VUE来做的,所以: changeFace:function(){ var IMAGE_UNSPECIFIED = "image/*"; //相册 ...

  3. AppScan扫描建议 问题集

    1.1        AppScan扫描建议 若干问题的补救方法在于对用户输入进行清理. 通过验证用户输入未包含危险字符,便可能防止恶意的用户导致应用程序执行计划外的任务,例如:启动任意 SQL 查询 ...

  4. Python3中关于下划线变量和命名的总结

    变量 #!-*-coding:utf-8-*- #__author__ = 'ecaoyng' # # 变量 #_xxx,单下划线开头的变量,标明是一个受保护(protected)的变量,原则上不允许 ...

  5. 9.7 Django 书单列表页面

    昨天的迭代版本,增加了编辑出版社,编辑列表,增添了返回页面! 具体的看 github : https://github.com/TrueNewBee/pythonDemo 看一下效果图: 整体来说还是 ...

  6. 关于jquery的css的一些知识

    Query实例CSS 样式表动态选择本实例主要说的还是jquery的选择器,关于jquery的css的一些知识用类似 $("li").css("cursor", ...

  7. ELK(使用RPM包安装配置ELK)

    1,安装环境查看 2,下载rmp包 下载地址:https://www.elastic.co/cn/downloads 分别下载最新rmp包 elasticsearch-6.2.4.rpm logsta ...

  8. ububtu16.04下安装protobuf

    重新下载protobuf,我下载的时最新的protobuf-all-3.5.1.tar.gz   protobuf网址:https://github.com/google/protobuf/relea ...

  9. Spyer中添加一些常用包的方法

    我用的是Anaconda中的Spyer编译,在导入包pyaudio时,发现找不到,需要手工导入.可以打开Anacoda promt,查看已经安装的包名用:pip list Spyer中的所有包在这里安 ...

  10. are not called implicitly

    php.net <?php class BaseClass{ function __construct() { print "In BaseClass constructor<b ...