遇到可变字符串修改的比较频繁的时候,考虑使用IO模块
>>> a = 'hello,sxt'
>>> import io
>>> sio = io.StringIO(a)
>>> sio
<_io.StringIO object at 0x00000252491B8280>
>>> sio.getvalue()
'hello,sxt'
>>> sio.seek(7)
7
>>> sio.write('g')
1
>>> sio.getvalue()
'hello,sgt'
>>> a = 4
>>> 3>> a = 0b11001
>>> b = 0b01000
>>> a
25
>>> b
8
>>> c = a|b
>>> c
25
>>> bin(c)
'0b11001'
>>> bin(a|b)
'0b11001'
>>> bin(a^b) #异操作,相同为0,相异为1
'0b10001'
>>> a=3
>>> a<<2 #左移×4
12
>>> a>>2 #右移除4
0
>>> a = [x*2 for x in range(5)]
>>> a
[0, 2, 4, 6, 8]
>>> a = [x*2 for x in range(100)if x%9==0]#通过if过滤数据
>>> a
[0, 18, 36, 54, 72, 90, 108, 126, 144, 162, 180, 198]
一般只在列表尾部增加或删除
和前面一样
>>> a = [20,10,30,40]
>>> a.sort()
>>> a
[10, 20, 30, 40]
>>> a.sort(reverse=True)
>>> a
[40, 30, 20, 10]
>>> import random
>>> random.shuffle(a) #随机打乱
>>> a
[30, 10, 20, 40]
>>> a
[30, 10, 20, 40]
>>> a
[30, 10, 20, 40]
>>> id(a)
2552437072000
>>> a = sorted(a)
>>> a
[10, 20, 30, 40]
>>> id(a)
2552437006016
>>> a = sorted(a,reverse=True)
>>> a
[40, 30, 20, 10]
>>> a
[40, 30, 20, 10]
>>> c = reversed(a)
>>> c
>>> list(c)
[10, 20, 30, 40]
>>> list(c) #迭代器只能用1次
[]
max和min
sum 只针对数值型列表
列表套列表
>>> a = [
['小王','584612','bj'],
['小de','58ed612','bj'],
['小cr','5846ed12','bj'],
]
>>> a
[['小王', '584612', 'bj'], ['小de', '58ed612', 'bj'], ['小cr', '5846ed12', 'bj']]
>>> a[0]
['小王', '584612', 'bj']
>>> a[0][0]
'小王'
#zip list可用于把两个列表合成一个字典
>>> a = [10,20,30]
>>> b = [4,5,7]
>>> zip(a,b)
>>> d = zip(a,b)
>>> list(d)
[(10, 4), (20, 5), (30, 7)]
>>> s = (x*2 for x in range(5))
>>> s
at 0x0000025249157900>
>>> tuple(s)
(0, 2, 4, 6, 8)
>>> tuple(s)
()
>>> s = (x*2 for x in range(5))
>>> s.__next__()
0
>>> s.__next__()
2
>>> s.__next__()
4
>>> s.__next__()
6
>>> s.__next__()
8
>>> s.__next__()
Traceback (most recent call last):
File "", line 1, in
s.__next__()
StopIteration
与整数和字符串一样,元组可以作为字典的键,列表则永远不能作为字典的键使用