打印列表元素
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(numbers)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
打印列表元素 python3
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(*numbers)
0 1 2 3 4 5 6 7 8 9
打印列表元素 python2
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
for number in numbers: print(number)
0
1
2
3
4
5
6
7
8
9
列表元素扩大2倍(使用列表推导式 list comprehension)
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
double = [number * 2 for number in numbers]
print(double)
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
列表逆序 反向1
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
reverse = list(reversed(numbers))
print(reverse)
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
列表逆序 反向2
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
reverse2 = numbers[::-1]
print(reverse2)
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
删除列表中多个元素
indexes = [2, 4, 3]
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
for index in sorted(indexes, reverse=True): del numbers[index]
print(numbers)
[0, 1, 5, 6, 7, 8, 9]
返回列表中最小值/最大值下标
numbers = [5, 2, 1, 6, 4]
index_min = min(range(len(numbers)), key = numbers.__getitem__)
index_max = max(range(len(numbers)), key = numbers.__getitem__)
print(index_min, numbers[index_min])
print(index_max, numbers[index_max])
2 1
3 6
删除列表中冗余元素(不保持原序)
numbers = [2, 2, 3, 3, 4, 1]
new = list(set(numbers))
print(new)
[1, 2, 3, 4]
删除列表中冗余元素(保持原序)
from collections import OrderedDict
numbers = [2, 2, 3, 3, 4, 1]
new = list(OrderedDict.fromkeys(numbers).keys())
print(new)
[2, 3, 4, 1]
统计列表中元素出现的次数
from collections import Counter
numbers = [1, 2, 3, 1, 2, 3, 2, 2, 3, 4, 5, 1]
print(Counter(numbers))
Counter({2: 4, 1: 3, 3: 3, 4: 1, 5: 1})
统计列表中出现次数最多的元素 1
numbers = [1, 2, 3, 1, 2, 3, 2, 2, 3, 4, 5, 1]
print(max(set(numbers), key = numbers.count))
2
统计列表中出现次数最多的元素 2
from collections import Counter
numbers = [1, 2, 3, 1, 2, 3, 2, 2, 3, 4, 5, 1]
cnt = Counter(numbers)
print(cnt.most_common(1))
[(2, 4)]
如何在一个列表中插入另一个列表
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
inserts = [2.3, 2.5]
numbers[3:3] = inserts
print(numbers)
[0, 1, 2, 2.3, 2.5, 3, 4, 5, 6, 7, 8, 9]
如何判断一个列表中的元素是否在另一个列表中
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
_in = [5, 12]
_not_in = [111, 222]
print(any(_ in numbers for _ in _in))
print(any(_ in numbers for _ in _not_in))
True
False
如何取列表中的奇数位/偶数位元素
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
odd = [_ for index,_ in enumerate(numbers) if index % 2]
print(odd)
[1, 3, 5, 7, 9]
列表中嵌套列表,如何展开
import itertools
numbers = [[0], [1, 2], ['3', 4, 5, 6], ['7', '8'], [9]]
list(itertools.chain.from_iterable(numbers))
[0, 1, 2, '3', 4, 5, 6, '7', '8', 9]
列表 zip
numbers = [1, 2, 3]
chars = ['a', 'b', 'c']
z = zip(numbers, chars)
list(z)
[(1, 'a'), (2, 'b'), (3, 'c')]
列表 unzip
numbers = [1, 2, 3]
chars = ['a', 'b', 'c']
z = zip(numbers, chars)
z2 = zip(*z)
list(z2)
[(1, 2, 3), ('a', 'b', 'c')]
列表拼接字符串
string = ['Python', 'is', 'YYDS']
' '.join(string)
'Python is YYDS'
列表拷贝(浅拷贝 Shadow Copy)(深拷贝 Deep Copy)
= 浅拷贝:值相同,地址相同
copy 浅拷贝:值相同,地址不同
deep copy:值相同,地址不同
a = [1, 2, 3]
b = a
b[0] = 0
print(id(a), a)
print(id(b), b)
print('---')
a = [1, 2, 3]
b = a[:]
b[0] = 0
print(id(a), a)
print(id(b), b)
print('---')
a = [1, 2, 3]
b = a.copy()
print(id(a), a)
print(id(b), b)
b[0] = 7
print(id(a), a)
print(id(b), b)
print('---')
from copy import deepcopy
a = [1, 2, 3]
b = deepcopy(a)
print(id(a), a)
print(id(b), b)
b[0] = 8
print(id(a), a)
print(id(b), b)
1896156101440 [0, 2, 3]
1896156101440 [0, 2, 3]
---
1896156101696 [1, 2, 3]
1896156101120 [0, 2, 3]
---
1896156102528 [1, 2, 3]
1896156102080 [1, 2, 3]
1896156102528 [1, 2, 3]
1896156102080 [7, 2, 3]
---
1896156101440 [1, 2, 3]
1896156101632 [1, 2, 3]
1896156101440 [1, 2, 3]
1896156101632 [8, 2, 3]
![](https://i-blog.csdnimg.cn/blog_migrate/ad6ea35a7e71a66660672ce130193200.jpeg)
-- EOF --