列表(list)
列表是一个序列
与字符串类似,列表 是由多个值组成的序列。在字符串中,每个值都是字符; 在列表中,值可以是任何数据类型。列表中的值称为 元素(element) ,有时也被称为 项(item) 。
[10, 20, 30, 40]
['crunchy frog', 'ram bladder', 'lark vomit']
['spam', 2.0, 5, [10, 20]]
>>> cheeses = ['Cheddar', 'Edam', 'Gouda']
>>> numbers = [42, 123]
>>> empty = []
>>> print(cheeses, numbers, empty)
['Cheddar', 'Edam', 'Gouda'] [42, 123] []
# 列表是可变的
>>> cheeses[0]
'Cheddar'
>>> numbers = [42, 123]
>>> numbers[1] = 5
>>> numbers
[42, 5]
# in运算符
>>> cheeses = ['Cheddar', 'Edam', 'Gouda']
>>> 'Edam' in cheeses
True
>>> 'Brie' in cheeses
False
# 遍历列表
for cheese in cheeses:
print(cheese)
# 如果你想要写入或者更新列表中的元素,你需要通过下标访问。
for i in range(len(numbers)):
numbers[i] = numbers[i] * 2
# 对一个空列表执行for循环时,将不会执行循环的主体:
for x in []:
print('This never happens.')
列表操作
# +运算符拼接多个列表:
>>> a = [1, 2, 3]
>>> b = [4, 5, 6]
>>> c = a + b
>>> c
[1, 2, 3, 4, 5, 6]
# 运算符 * 以给定次数的重复一个列表:
>>> [0] * 4
[0, 0, 0, 0]
>>> [1, 2, 3] * 3
[1, 2, 3, 1, 2, 3, 1, 2, 3]
# 列表切片
# 如果你两者都省略,切片就是整个列表的一个拷贝。
>>> t = ['a', 'b', 'c', 'd', 'e', 'f']
>>> t[1:3]
['b', 'c']
>>> t[:4]
['a', 'b', 'c', 'd']
>>> t[3:]
['d', 'e', 'f']
# 由于列表是可变的,通常在修改列表之前,对列表进行拷贝是很有用的。
# 切片运算符放在赋值语句的左边时,可以一次更新多个元素:
>>> t = ['a', 'b', 'c', 'd', 'e', 'f']
>>> t[1:3] = ['x', 'y']
>>> t
['a', 'x', 'y', 'd', 'e', 'f']
列表方法
# append 添加一个新元素到列表的末端:
>>> t = ['a', 'b', 'c']
>>> t.append('d')
>>> t
['a', 'b', 'c', 'd']
# extend将接受一个列表作为参数,并将其其中的所有元素添加至目标列表中:
>>> t1 = ['a', 'b', 'c']
>>> t2 = ['d', 'e']
>>> t1.extend(t2)
>>> t1
['a', 'b', 'c', 'd', 'e']
# sort将列表中的元素从小到大进行排序:
>>> t = ['d', 'c', 'e', 'b', 'a']
>>> t.sort()
>>> t
['a', 'b', 'c', 'd', 'e']
映射、筛选和归并:
def add_all(t):
total = 0
for x in t:
total += x
return total
>>> t = [1, 2, 3]
>>> sum(t)
6
def capitalize_all(t):
res = []
for s in t:
res.append(s.capitalize()) # 大写字符
return res
def only_upper(t):
res = []
for s in t:
if s.isupper():
res.append(s)
return res
内建内置函数 sum。
>>> t = [1, 2, 3]
>>> sum(t)
6
删除元素
pop修改列表,并返回被移除的元素。如果你不提供下标,它将移除并返回最后一个元素。
>>> t = ['a', 'b', 'c']
>>> x = t.pop(1)
>>> t
['a', 'c']
>>> x
'b'
# 如果你不需要被移除的元素,可以使用 del 运算符
>>> t = ['a', 'b', 'c']
>>> del t[1]
>>> t
['a', 'c']
# 如果你知道要删除的值(但是不知道其下标),你可以使用 remove
>>> t = ['a', 'b', 'c']
>>> t.remove('b')
>>> t
['a', 'c']
# 要移除多个元素,你可以结合切片索引使用 del
>>> t = ['a', 'b', 'c', 'd', 'e', 'f']
>>> del t[1:5]
>>> t
['a', 'f']
列表和字符串
可以使用 list 将一个字符串转换为字符的列表:
>>> s = 'spam'
>>> t = list(s)
>>> t
['s', 'p', 'a', 'm']
# 如果你想将一个字符串分割成一些单词,你可以使用 split 方法
>>> s = 'pining for the fjords'
>>> t = s.split()
>>> t
['pining', 'for', 'the', 'fjords']
# 可以提高一个叫做 分隔符(delimiter) 的可选参数,指定什么字符作为单词之间的分界线。
>>> s = 'spam-spam-spam'
>>> delimiter = '-'
>>> t = s.split(delimiter)
>>> t
['spam', 'spam', 'spam']
# join的功能和 split 相反。它将一个字符串列表的元素拼接起来。
# join 是一个字符串方法
>>> t = ['pining', 'for', 'the', 'fjords']
>>> delimiter = ' '
>>> s = delimiter.join(t)
>>> s
'pining for the fjords'
对象和值
为了查看两个变量是否指向同一个对象,你可以使用 is 运算符
>>> a = 'banana'
>>> b = 'banana'
>>> a is b
True
# 内置函数 sorted ,它将返回一个新的已排序的列表,原列表将保持不变。
>>> t2 = sorted(t)
>>> t
[3, 1, 2]
>>> t2
[1, 2, 3]
>>> t = [3, 1, 2]
>>> t2 = t[:]
>>> t2.sort()
>>> t
[3, 1, 2]
>>> t2
[1, 2, 3]
网友评论