博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Intro to Python for Data Science Learning 2 - List
阅读量:5160 次
发布时间:2019-06-13

本文共 2603 字,大约阅读时间需要 8 分钟。

List

from:

 

-----------------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 linecommand1; command2# Separate linescommand1command2 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.

 

转载于:https://www.cnblogs.com/keepSmile/p/7694061.html

你可能感兴趣的文章
Reveal 配置与使用
查看>>
Java中反射的学习与理解(一)
查看>>
C语言初学 俩数相除问题
查看>>
B/S和C/S架构的区别
查看>>
[Java] Java record
查看>>
jQuery - 控制元素显示、隐藏、切换、滑动的方法
查看>>
postgresql学习文档
查看>>
Struts2返回JSON数据的具体应用范例
查看>>
js深度克隆对象、数组
查看>>
socket阻塞与非阻塞,同步与异步
查看>>
团队工作第二天
查看>>
System类
查看>>
tableView
查看>>
Happy Great BG-卡精度
查看>>
Xamarin Visual Studio不识别JDK路径
查看>>
菜鸟“抄程序”之道
查看>>
Ubuntu下关闭防火墙
查看>>
TCP/IP 邮件的原理
查看>>
原型设计工具
查看>>
windows下的C++ socket服务器(4)
查看>>