- len
返回列表中元素的个数,同样适用于元组、字典、集合、字符串
def len(*args, **kwargs): # real signature unknown
""" Return the number of items in a container. """
pass
>>> _str = "1234567890"
>>> len(_str)
10
>>> _tuple = tuple(_str)
>>> len(_tuple)
10
>>> _list = list(_str)
>>> len(_list)
10
- zip
返回多个可遍历变量组合后的元组列表
class zip(object):
"""
zip(iter1 [,iter2 [...]]) --> zip object
Return a zip object whose .__next__() method returns a tuple where
the i-th element comes from the i-th iterable argument. The .__next__()
method continues until the shortest iterable in the argument sequence
is exhausted and then it raises StopIteration.
>>> s1 = "12345"
>>> s2 = "67890"
>>> for i, j in zip(s1, s2):
... print(i, j)
...
1 6
2 7
3 8
4 9
5 0
- enumerate
用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中
class enumerate(object):
"""
Return an enumerate object.
iterable
an object supporting iteration
The enumerate object yields pairs containing a count (from start, which
defaults to zero) and a value yielded by the iterable argument.
enumerate is useful for obtaining an indexed list:
(0, seq[0]), (1, seq[1]), (2, seq[2]), ...
"""
>>> _str = "1234567890"
>>> for i, e in enumerate(_str):
... print(i, e)
...
0 1
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 0
引用:
[1]. https://blog.csdn.net/eacxzm/article/details/79778769
[2]. https://www.cnblogs.com/wzwpython/p/11549239.html