列表
序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推。序列都可以进行的操作包括索引,切片,加,乘,检查成员。
创建一个列表
list_name = [elem1,elem2,…] 逗号隔开每个元素,外面为方括号
list_a = ['Baidu','Google','Chorme',2011,1999]
list_num = [9,8,7,6,5,4,3,2,1,0]
list_alpha = ['a','b','c','d','e','f','g']
print(list_a)
print(list_num)
print(list_alpha)
'''
['Baidu', 'Google', 'Chorme', 2011, 1999]
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
['a', 'b', 'c', 'd', 'e', 'f', 'g']
'''
range() 可以创建数值列表
mylist = list(range(10))
print(mylist)
'''
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
'''
访问列表中的值
list[index] ,注意index是从0开始
list_a = ['Baidu','Google','Chorme',2011,1999]
list_num = [9,8,7,6,5,4,3,2,1,0]
list_alpha = ['a','b','c','d','e','f','g']
print('list_a[2]:',list_a[2])
print('list_num[4]:',list_num[4])
print('list_alpha[-3]:',list_alpha[-3])
'''
list_a[2]: Chorme
list_num[4]: 5
list_alpha[-3]: e
'''
更新列表
list_a = ['Baidu','Google','Chorme',2011,1999]
print('old list_a[2]:',list_a[2])
list_a[2] = 'None'
print('new list_a[2]:',list_a[2])
'''
old list_a[2]: Chorme
new list_a[2]: None
'''
删除列表元素
del:根据索引位置来删除,也可以删除整个数据对象
list_a = ['Baidu','Google','Chorme',2011,1999]
print('old list_a[2]:',list_a[2])
del list_a[2]
print('new list_a[2]:',list_a[2])
'''
old list_a[2]: Chorme
new list_a[2]: 2011
'''
remove:删除单个元素,删除首个符合条件的元素,按值删除
>>> str=[1,2,3,4,5,2,6]
>>> str.remove(2)
>>> str
[1, 3, 4, 5, 2, 6]
pop: 删除单个或多个元素,按位删除,根据索引删除
>>> str=[1,2,3,4,5,2,6]
>>> str.pop(5)
2
>>> str
[1, 2, 3, 4, 5, 6]
列表截取与拼接
列表截取:
list_a = ['Baidu','Google','Chorme',2011,1999]
print('list_a[1]-list_a[3]:',list_a[1:4])
>>>list_a[1]-list_a[3]: ['Google', 'Chorme', 2011]
列表拼接:
list_num = [9,8,7,6,5,4,3,2,1,0]
print('old list_num:',list_num)
list_num += [x**2 for x in list_num]
print('new list_num:',list_num)
>>>old list_num: [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
>>>new list_num: [9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 81, 64, 49, 36, 25, 16, 9, 4, 1, 0]
列表拷贝:
列表拷贝有深拷贝和浅拷贝
深拷贝: 修改拷贝的列表不会影响被拷贝列表
- N=M[:]
- N = M.copy()
实例1:
list_a = ['Baidu','Google','Chorme',2011,1999]
list_copy = list_a[:]
print(list_copy)
list_copy[0] = "Hello" #不会修改list_a
print(list_copy)
print(list_a)
'''
['Baidu', 'Google', 'Chorme', 2011, 1999]
['Hello', 'Google', 'Chorme', 2011, 1999]
['Baidu', 'Google', 'Chorme', 2011, 1999]
'''
实例2:
list_a = ['Baidu','Google','Chorme',2011,1999]
list_copy = list_a.copy()
print(list_copy)
list_copy[0] = "Hello"
print(list_copy)
print(list_a)
'''
['Baidu', 'Google', 'Chorme', 2011, 1999]
['Hello', 'Google', 'Chorme', 2011, 1999]
['Baidu', 'Google', 'Chorme', 2011, 1999]
'''
浅拷贝:修改拷贝的列表会影响被拷贝列表
- M=[N,L,P]
- M = N
实例1:
list_a = ['Baidu','Google','Chorme',2011,1999]
list_b = ['China','America','French',"Germany"]
list_copy = [list_a,list_b]
print(list_copy)
print(list_copy[0][0],list_copy[1][3],end = " ")
list_copy[0][0] = "Hello"
print("\n")
print(list_copy)
print(list_a) #注意看输出,list_a也被改变了
'''
[['Baidu', 'Google', 'Chorme', 2011, 1999], ['China', 'America', 'French', 'Germany']]
Baidu Germany
[['Hello', 'Google', 'Chorme', 2011, 1999], ['China', 'America', 'French', 'Germany']]
['Hello', 'Google', 'Chorme', 2011, 1999]
'''
实例2:
list_a = ['Baidu','Google','Chorme',2011,1999]
list_copy = list_a
print(list_copy)
list_copy[0] = "Hello" #list_a也会被修改
print(list_copy)
print(list_a)
'''
['Baidu', 'Google', 'Chorme', 2011, 1999]
['Hello', 'Google', 'Chorme', 2011, 1999]
['Hello', 'Google', 'Chorme', 2011, 1999]
'''
列表函数&方法
列表包含以下函数:
len(list) 返回列表元素个数:
list_num = [9,8,7,6,5,4,3,2,1,0]
print(len(list_num))
>>>10
max(list) 返回列表元素最大元素值:
list_num = [9,8,7,6,5,4,3,2,1,0]
print(max(list_num))
>>>9
min(list) 返回列表元素最小元素值:
list_num = [9,8,7,6,5,4,3,2,1,0]
print(min(list_num))
>>>0
list(seq) 将元组转换为列表:
Tuple1 = (2,5,27,28)
print(Tuple1)
list = list(Tuple1)
print(list)
>>>(2, 5, 27, 28)
>>>[2, 5, 27, 28]
列表包含以下方法:
list.append(obj) 在列表尾添加新的对象
list_num = [9,8,7,6,5,4,3,2,1,0]
print('old list_num:',list_num)
list_num.append('2019')
print('new list_num:',list_num)
>>>old list_num: [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
>>>new list_num: [9, 8, 7, 6, 5, 4, 3, 2, 1, 0, '2019']
list.count(obj) 统计某个元素在列表中出现的次数
list_num = [9,8,6,6,6,4,3,2,6,0]
print(list_num.count(6))
>>>4
list.extend(seq) 在列表末尾一次性追加另一个序列中的多个值
list_num = [9,8,6,6,6,4,3,2,6,0]
list_alpha = ['a','b','c','d','e','f','g']
print('old list_num:',list_num)
list_num.extend(list_alpha)
print('new list_num:',list_num)
'''
old list_num: [9, 8, 6, 6, 6, 4, 3, 2, 6, 0]
new list_num: [9, 8, 6, 6, 6, 4, 3, 2, 6, 0, 'a', 'b', 'c', 'd', 'e', 'f', 'g']
'''
list.index(obj) 在列表中找出某个值第一次匹配到的索引位置
list_num = [9,8,6,6,6,4,3,2,6,0]
print(list_num.index(4))
print(list_num.index(15)) #没有15值将会爆错
>>>5
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
print(list_num.index(15))
ValueError: 15 is not in list
list.insert(index,obj) 将对象插入列表
list_num = [9,8,6,6,6,4,3,2,6,0]
list_num.insert(6,999)
print(list_num)
list_num.insert(80,11) #index超过索引范围将放置于列表末尾
print(list_num)
'''
[9, 8, 6, 6, 6, 4, 999, 3, 2, 6, 0]
[9, 8, 6, 6, 6, 4, 999, 3, 2, 6, 0, 11]
'''
list.pop(index = -1) 移除列表中的一个元素,并且返回该元素的值,默认最后一个元素
list_num = [9,8,6,6,6,4,3,2,6,0]
list_num.pop()
print(list_num)
list_num.pop(6)
print(list_num)
'''
[9, 8, 6, 6, 6, 4, 3, 2, 6]
[9, 8, 6, 6, 6, 4, 2, 6]
'''
方法 | 描述 |
---|---|
list.remove(obj) | 移除列表中某个值的第一个匹配项 |
list.reverse() | 反向列表中元素 |
list.sort(key=None,reverse=False) | 对原列表进行排序,reverse = True 降序, reverse = False 升序(默认) |
list.clear() | 清空列表 |
list.copy() | 复制列表,深复制 |
list_num = [9,8,6,6,6,4,3,2,6,0]
list_num.remove(8)
print(list_num)
list_num.reverse()
print(list_num)
list_num.sort()
print(list_num)
list_num.sort(reverse = True)
print(list_num)
list_num.clear()
print(list_num)
'''
[9, 6, 6, 6, 4, 3, 2, 6, 0]
[0, 6, 2, 3, 4, 6, 6, 6, 9]
[0, 2, 3, 4, 6, 6, 6, 6, 9]
[9, 6, 6, 6, 6, 4, 3, 2, 0]
[]
'''
通过指定列表中的元素排序来输出列表:
list_num = [(9,8,6),(6,6,4),(3,2,6),(0,41,2)]
def takeSecond(elem):
return elem[1]
def takeThird(elem):
return elem[2]
list_num.sort()
print(list_num)
list_num.sort(key = takeSecond)
print(list_num)
list_num.sort(key = takeThird)
print(list_num)
'''
[(0, 41, 2), (3, 2, 6), (6, 6, 4), (9, 8, 6)]
[(3, 2, 6), (6, 6, 4), (9, 8, 6), (0, 41, 2)]
[(0, 41, 2), (6, 6, 4), (3, 2, 6), (9, 8, 6)]
'''
元组tuple,字典dict,列表list之间的转换
元组转换:不能转换为字典
>>> tup=(1, 2, 3, 4, 5,6,7,8)
>>> tup.__str__()
'(1, 2, 3, 4, 5, 6, 7, 8)'
>>> list(tup)
[1, 2, 3, 4, 5, 6, 7, 8]
字典转换:
>>> dict = {'name': 'Zara', 'age': 7}
>>> str(dict)
"{'name': 'Zara', 'age': 7}"
>>> tuple(dict)
('name', 'age')
>>> tuple(dict.values())
('Zara', 7)
>>> list(dict)
['name', 'age']
列表转换:
>>> nums=[1, 3, 5, 7, 9, 11, 13];
>>> str(nums)
'[1, 3, 5, 7, 9, 11, 13]'
>>> tuple(nums)
(1, 3, 5, 7, 9, 11, 13)