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. 检测硬件RDMA卡是否存在

    1.检查网卡是否安装成功: # lspci | grep Mellanox 83:00.0 Ethernet controller: Mellanox Technologies MT27710 Fam ...

  2. Kafka usecase

    h1, h2, h3, h4, h5, h6, p, blockquote { margin: 5px; padding: 5; } body { font-family: "Helveti ...

  3. 基于spring-cloud的微服务(3)eureka的客户端如何使用IP地址来进行注册

    例子中和我写的代码里,使用的spring-boot的版本是2.0 Eureka的客户端默认是使用hostname来进行注册的,有的时候,hostname是不可靠的,需要使用IP地址来进行注册 name ...

  4. CentOS 6.8下Apache绑定多个域名的方法

    如何通过设置Apache的http.conf文件,进行多个域名的绑定(假设我们要绑定的域名是discuz11.com和discuz22.com,独立IP为25.25.25.25). 域名/IP地址 域 ...

  5. php中调用这个功能可以在web页面中显示hello world这个经典单词

    php程序写的时间长了,自然对他所提供的功能了如指掌,他所提供的一大堆功能,真是觉得很好用,但有时候会发现php也缺少一些功能,自己总是会产生为php添加一些自定义的功能的想法.久而久之,终于今天憋不 ...

  6. opencv学习网站

    强烈推荐一个老外的网站,pyimagesearch 网址:https://www.pyimagesearch.com/

  7. 伸展树(Splay Tree)进阶 - 从原理到实现

    目录 1 简介 2 基础操作 2.1 旋转 2.2 伸展操作 3 常规操作 3.1 插入操作 3.2 删除操作 3.3 查找操作 3.4 查找某数的排名.查找某排名的数 3.4.1 查找某数的排名 3 ...

  8. 20144306《网络对抗》MAL_免杀原理与实践

    一.基础问题回答 1.杀软是如何检测出恶意代码的? (1)特征码:类似于人的生物特征,恶意代码可能会包含一段或多端数据能代表其特征.杀软一般会对文件内容进行静态扫描,将文件内容与特征库进行匹配,来检测 ...

  9. bokeyuan_python文章爬去入mongodb读取--LOWBIPROGRAMMER

    # -*- coding: utf-8 -*- import requests,os from lxml import etree from pymongo import * class Boke(o ...

  10. RESTful URL设计指南(转)

    add by zhj: <RESTful Web Services Cookbook>这本书详细介绍了RESTFUL API的设计. 一般来说,一个好的URL,简单明了.这里有一个问题,对 ...