python list tricks / pythonic list (CRUD) / python列表技法

本文详细介绍了Python列表的各种操作,包括打印、反转、删除元素、查找最值下标、去重、统计元素出现次数等,并通过实例展示了如何插入元素、判断元素存在性、拼接字符串以及列表的浅拷贝与深拷贝。这些操作对于理解和使用Python列表至关重要。
摘要由CSDN通过智能技术生成
打印列表元素
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]
欢迎交流

-- EOF --

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值