序列
'is和is not判断是否是同一个函数'
>>> x='123'
>>> y='123'
>>> x is y
True
>>> x=[1,2,3]
>>> y=[1,2,3]
>>> x is y
False
'in 和 not in判断B是不是在A里面'
>>> "1" in "1,2,3"
True
>>> "1"not in "1,2,3"
False
'del删除一个或多个指定的对象'
>>> x='hahaha'
>>> y='1,2,3'
>>> del x,y
>>> x
Traceback (most recent call last):
File "<pyshell#19>", line 1, in <module>
x
NameError: name 'x' is not defined
>>> y
Traceback (most recent call last):
File "<pyshell#20>", line 1, in <module>
y
NameError: name 'y' is not defined
>>> x=[1,2,3,4,5]
>>> del x[1:4]
>>> x
[1, 5]
>>> '也可以用切片的方法'
'也可以用切片的方法'
>>> x=[1,2,3,4,5]
>>>> x[1:4]=[]
>>> x
[1, 5]
'清空列表方法'
>>> x=[1,2,3,4,5]
>>> x.clear()
>>> x
[]
>>> y=[1,2,3,4,5]
>>> del y[:]
>>> y
[]
列表、元组、字符串相互转换
>>> list('vookii')
['v', 'o', 'o', 'k', 'i', 'i']
>>> list((1,2,3,4))
[1, 2, 3, 4]
>>> tuple([1,2,3,4])
(1, 2, 3, 4)
>>> tuple((1,2,3,4))
(1, 2, 3, 4)
>>> tuple('vookii')
('v', 'o', 'o', 'k', 'i', 'i')
>>> str([1,2,3,4,])
'[1, 2, 3, 4]'
>>> str((1,2,3,4))
'(1, 2, 3, 4)'
'判断最大与最小值min与max'
>>> x=[1,0,0,8,6]
>>>> min(x)
0
>>> y='vookii'
>>> min(y)
'i'
>>> z=[]
>>>> min(z)
Traceback (most recent call last):
File "<pyshell#54>", line 1, in <module>
min(z)
ValueError: min() arg is an empty sequence
>>> min(z,default='none')
'none'
>>> min(1,0,0,8,6)
0
>>> max(1,0,0,8,6)
8
>>> 'len()&sum()'
'len()&sum()'
>>> len(range(2**10))
1024
>>> s=[1,2,3,0,6]
>>> sum(s)
12
>>> sum(s,start=120)
132
'sorted() & reversed()'
>>> s=[1,2,3,0,6]
>>> sorted(s)
[0, 1, 2, 3, 6]
>>> s
[1, 2, 3, 0, 6]
>>> s.sort()
>>> s
[0, 1, 2, 3, 6]
>>> 'sorted不改变数组本身的顺序,sort改变数组本身'
>>>> sorted(s,reverse=True)
[6, 3, 2, 1, 0]
>>> y=['real','vookii','is','me']
>>>> sorted(y)
['is', 'me', 'real', 'vookii']
>>> '根据字母顺序排序'
>>>> sorted(y,key=len)
['is', 'me', 'real', 'vookii']
>>> y.sort(key=len)
>>> y
['is', 'me', 'real', 'vookii']
>>> '根据字母数量排序'
>>>> m=[1,2,3,5,6,9]
>>> reversed(m)
<list_reverseiterator object at 0x03874400>
>>> list(reversed(s))
[6, 3, 2, 1, 0]
>>> m.reverse()
>>> m
[9, 6, 5, 3, 2, 1]
>>> list(reversed('vookii'))
['i', 'i', 'k', 'o', 'o', 'v']
>>> list(reversed((1,0,0,8,6)))
[6, 8, 0, 0, 1]
>>> list(reversed(range(0,10)))
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
enumerate()
enumerate()函数用于返回一个枚举对象,他的功能是将可迭代对象中的每个元素从0开始的序列号共同构成一个二元组的列表。
>>> jijie=['chun','xia','qiu','dong']
>>> enumerate(jijie)
<enumerate object at 0x03871B08>
>>> list(enumerate(jijie))
[(0, 'chun'), (1, 'xia'), (2, 'qiu'), (3, 'dong')]
>>> list(enumerate(jijie,10))
[(10, 'chun'), (11, 'xia'), (12, 'qiu'), (13, 'dong')]
zip()
zip()函数用于创建一个聚合多个可迭代对象的迭代器。他将会作为参数传入的每个可迭代对象的每个元素依次组合成元组,即第i个元素组包含来自每个参数的第i个元素。
>>> x=[1,2,3]
>>> y=[4,5,6]
>>> m=zip(x,y)
>>> m
<zip object at 0x034E5708>
>>> list(m)
[(1, 4), (2, 5), (3, 6)]
>>> z='vookii'
>>> m=zip(x,y,z)
>>> list(m)
[(1, 4, 'v'), (2, 5, 'o'), (3, 6, 'o')]
>>> '当输入的元素长度不同时按最短的合并'
'不漏掉任何一个元素的方法'
>>> import itertools
>>> m=itertools.zip_longest(x,y,z)
>>> list(m)
[(1, 4, 'v'), (2, 5, 'o'), (3, 6, 'o'), (None, None, 'k'), (None, None, 'i'), (None, None, 'i')]
map()
map()函数会根据提供的函数对制定的可迭代对象的每个元素进行运算,并将返回运算结果的迭代器。
>>> n=map(ord,'vookii')
>>>> list(n)
[118, 111, 111, 107, 105, 105]
>>> 'ord函数是获得vookii每个字母的序号'
>>>> n=map(pow,[2,4,6],[1,2,3])
>>> list(n)
[2, 16, 216]
'等价于'
>>> [pow(2,1),pow(4,2),pow(6,3)]
[2, 16, 216]
'如果遇到各数组元素个数不同时选最短的输出'
>>>
filter()
filter()函数会根据提供的函数对指定的可迭代对象的每个元素进行运算,并将运算结果为真的元素,以迭代器的形式返回。
>>> list(filter(str.islower,'Vookii'))
['o', 'o', 'k', 'i', 'i']
迭代器vs可迭代对象*
一个迭代器肯定是一个可迭代对象;
可迭代对象可以重复使用而迭代器则是一次性的
>>> m=map(ord,'Vookii')
>>> for each in m:
print(each)
86
111
111
107
105
105
>>> list(m)
[]
>>> x=[1,2,3,4,5]
>>> y=iter(x)
>>> type(x)
<class 'list'>
>>> type(y)
<class 'list_iterator'>
>>> 'y是个迭代器,值取完就没有了'
'y是个迭代器,值取完就没有了'
>>> next(y)
1
>>> next(y)
2
>>> next(y)
3
>>> next(y)
4
>>> next(y)
5
>>> next(y)
Traceback (most recent call last):
File "<pyshell#150>", line 1, in <module>
next(y)
StopIteration
>>> m=iter(x)
>>> next(m,'值被取空了')
1
>>> next(m,'值被取空了')
2
>>> next(m,'值被取空了')
3
>>> next(m,'值被取空了')
4
>>> next(m,'值被取空了')
5
>>> next(m,'值被取空了')
'值被取空了'