Python列表

列表

一个数组中可以有字符串,也可以有数字

>>> spam = [['cat','bat'],[10,20,30]]
>>> spam[1]
[10, 20, 30]
>>> spam[0]
['cat', 'bat']
>>> spam[0][0]
'cat'
>>> spam[1][0]
10
>>> spam[0][0][0]
'c'
>>> spam[-1]
[10, 20, 30]

利用切片获取子列表

>>> spam
['a', 'b', 'c', 'd']
>>> spam[1:3]
['b', 'c']
>>> spam[1:-1]
['b', 'c']
>>> 

列表函数

len

>>> len(spam)
4

操作符 ‘*’,’+’运算

>>> ['a','c','d']+[1,2,3,4]
['a', 'c', 'd', 1, 2, 3, 4]
>>> (['a','c','d']+[1,2,3,4])*3
['a', 'c', 'd', 1, 2, 3, 4, 'a', 'c', 'd', 1, 2, 3, 4, 'a', 'c', 'd', 1, 2, 3, 4]

del从列表中删除

>>> spam
['a', 'b', 'c', 'd']
>>> del spam[1]
>>> spam
['a', 'c', 'd']

for循环和列表

>>> for i in [1,2,3]:
    print(spam[i])

in 和 not in 操作

>>> spam = ['a', 'b', 'c', 'd']
>>> spam
['a', 'b', 'c', 'd']
>>> a = 'a'
>>> a in spam
True
>>> temp = 'f'
>>> temp not in spam
True

多重赋值技巧

>>> spam = ['a', 'b', 'c', 'd']
>>> a,b,c,d = spam
>>> a
'a'
>>> spam[2:4] = ['1', '2']
>>> spam
['a', 'b', '1', '2']

方法

index()方法在列表中查找值

>>> spam = ['a','b','c']
>>> spam
['a', 'b', 'c']
>>> spam.index('a')
0

append()和insert()的使用

>>> spam
['a', 'b', 'c']
>>> spam.append(a)
>>> spam
['a', 'b', 'c', 'a']  #在列表最后添加项目
>>> spam.insert(1, 'temp')
>>> spam
['a', 'temp', 'b', 'c', 'a']  #在指定位置添加制定项

remove()方法从列表中删除值

>>> spam
['a', 'temp', 'b', 'c', 'a']
>>> spam.remove('temp')
>>> spam
['a', 'b', 'c', 'a']
>>> spam.remove('g')
Traceback (most recent call last):
  File "<pyshell#86>", line 1, in <module>
    spam.remove('g')
ValueError: list.remove(x): x not in list

sort()将列表中的值的排序

默认按照ASCII排序

>>> s = ['c', 'b', 'a', 1, 5, 3, 3.1]
>>> s.sort()
>>> s
[1, 3, 3.1, 5, 'a', 'b', 'c']

逆向排序

>>> s = [1, 4, 3.14, 3, 5]
>>> s.sort(reverse=True)
>>> s
[5, 4, 3.14, 3, 1]

按照字典排序

注:key = str.lower 是把所有元素当作小写·

>>> spam = ['a','Z','z','A']
>>> spam.sort(key=str.lower)
>>> spam
['a', 'A', 'Z', 'z']

元组数据类型

元组的内容是不可改变的,其次Python对元组进行了优化,使用速度更快

>>> sp = ('hello',99,3.14)
>>> type(sp)
<type 'tuple'>
>>> type(('hello',))
<class 'tuple'>
>>> type(('hello'))
<class 'str'>
>>> sp[1]
99
>>> sp[1] = 88
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment

使用list()和tuple()函数来转换类型

>>> list(('who','are','you'))
['who', 'are', 'you']
>>> list(('hello'))
['h', 'e', 'l', 'l', 'o']
>>> tuple(['hello','word'])
('hello', 'word')
>>> tuple(['hello'])
('hello',)

引用

在赋值时并未复制,而是指向同一个列表

>>> spam
[1, 5, 4, 2, 'hello', 'Z', 'z']
>>> chees = spam
>>> chees[2] = 'world'
>>> spam
[1, 5, 'world', 2, 'hello', 'Z', 'z']
>>> chees
[1, 5, 'world', 2, 'hello', 'Z', 'z']

传递应用(类似于引用调用)

def eggs(temp):
    temp.append('hello')
spam = [1,2,3]
eggs(spam)
print(spam)
#[1, 2, 3, 'hello']

copy()函数和deepcopy()函数

copy.copy() 可以用来复制列表和字典这样的可变值,而不只是复制引用

>>> import copy
>>> spam = [1,2,3]
>>> spam
[1, 2, 3]
>>> temp = copy.copy(spam)
>>> temp
[1, 2, 3]
>>> spam.append('hello')
>>> spam
[1, 2, 3, 'hello']
>>> temp
[1, 2, 3]
>>> temp.append('OK')
>>> spam,temp
([1, 2, 3, 'hello'], [1, 2, 3, 'OK'])

如果说列表中包含了列表,则应当使用copy.deepcopt()

>>> spam
[[1, 2, 3, 'OK'], 1, 2]
>>> temp = copy.deepcopy(spam)
>>> temp
[[1, 2, 3, 'OK'], 1, 2]

版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/02/22/python-list/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
海报
Python列表
列表 一个数组中可以有字符串,也可以有数字 >>> spam = [['cat','bat'],[10,20,30]] >>> spam[1] [10, 20, 30] >>> ……
<<上一篇
下一篇>>
文章目录
关闭
目 录