Python入门笔记:列表
用括号括[]
起来,用逗号,
分隔其中的元素。
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles)
print(bicycles[0])
print(bicycles[1])
print(bicycles[2])
print(bicycles[3])
print(bicycles[-1])
print(bicycles[-2])
print(bicycles[-3])
print(bicycles[-4])
负数索引表示倒数第几个元素。
向列表添加元素 list.append(something)
#往list末尾新增something list.append(n, something)
#往list的第n个位置添加something
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles)
bicycles.append('suzuki')
print(bicycles)
bicycles.insert(2, 'ducati')
print(bicycles)
删除列表元素 del list[n]
#删除list的第n个元素 poped = list.pop(n)
#删除list的第n个元素,n缺省表示删除末尾元素,poped存放被删除的元素 list.remove(something)
#删除list中第一个值为something的元素
bicycles = ['trek', 'cannondale', 'redline', 'specialized', 'redline', 'ducati' ]
print(bicycles)
del bicycles[2]
bicycles = ['trek', 'cannondale', 'redline', 'specialized', 'redline', 'ducati' ]
print(bicycles)
poped = bicycles.pop()
print(bicycles)
print(poped)
bicycles = ['trek', 'cannondale', 'redline', 'specialized', 'redline', 'ducati' ]
print(bicycles)
poped = bicycles.pop(1)
print(bicycles)
print(poped)
bicycles = ['trek', 'cannondale', 'redline', 'specialized', 'redline', 'ducati' ]
print(bicycles)
bicycles.remove('redline')
print(bicycles)
排序 list.sort()
#对list按字母顺序排列a-z list.sort(reverse=True)
#z-a sorted(list)
#返回排序好的列表a-z,不改变list的值 sorted(list, reverse=True)
bicycles = ['trek', 'cannondale', 'redline', 'specialized', 'redline', 'ducati' ]
print(bicycles)
print(sorted(bicycles))
print(bicycles)
print(sorted(bicycles,reverse=True))
print(bicycles)
bicycles.sort()
print(bicycles)
bicycles = ['trek', 'cannondale', 'redline', 'specialized', 'redline', 'ducati' ]
print(bicycles)
bicycles.sort(reverse=True)
列表倒序 list.reverse()
#倒序list
bicycles = ['trek', 'cannondale', 'Redline', 'specialized', 'redline', 'ducati' ]
print(bicycles)
bicycles.reverse()
print(bicycles)
len(list)
#返回list的长度
bicycles = ['trek', 'cannondale', 'Redline', 'specialized', 'redline', 'ducati' ]
print(bicycles)
print(len(bicycles))
遍历列表
magicians = ['alice', 'david', 'carolina']
for magician in magicians:
print(magician.title() + ", that was a great trick!")
print("I can't wait to see your next trick, " + magician.title() + ".\n")
print("Thank you, everyone. That was a great magic show!")
用缩进区分代码块
list(something)
#将something变为列表 range(a,b,c)
#从a到b并不包含b,步长为c,默认为1
for value in range(1,5):
print(value)
print(list(range(1,5)))
print(list(range(2,10,2)))
print(list(range(2,11,2)))
squares = []
for value in range(1,11):
squares.append(value**2)
print(squares)
min()
max()
sum()
用列表解析创建squares
squares = [value**2 for value in range(1,11)]
print(squares)
列表切片
和matlab类似,但步长只为1.
复制列表
直接看例子,就是不能用等号直接赋值
m = list(range(2,12))
print(m)
n = m[:]
print(n)
m.append(1234)
n.append(7890)
print("m = ")
print(m)
print("n = ")
print(n)
n = m
m.append(56)
print("m = ")
print(m)
print("n = ")
print(n)
n.append(1029)
print("m = ")
print(m)
print("n = ")
print(n)
元组
和列表类似,只不过用()
括起来。
元组中的元素不可修改,不过整个元组的值可以改变。
只有一个元素的话要在末尾加,
否则会变成单一的变量,非元组。
如果觉得此文章有用,点击这里,万分感谢。
Reference
[美]Eric Matthes著,袁国忠译. Python编程从入门到实践[M]. 北京:人民邮电出版社. 2016.7.
上一篇: 一大份薯条,可以盖高楼
下一篇: 妹子,你的有脚有点违背物理学