- zip:将可迭代的两个列表进行tuple
# zip
l1 = [1,2,3,4,5]
l2 = [6,7,8,9,10]
z = zip(l1,l2)
print(z)
for x,y in z:
print(x,y)
- enumerate:对于可迭代的对象中的每一个元素,配上一个索引
# enumerate
l1 = [1,2,3,4,5]
# start 从哪个开始索引
em = enumerate(l1,start=1)
print(em)
for x,y in em:
print(x,y)
- collections 模块
- namedtuple
- deque
# collections
# namedtuple
import collections
Point = collections.namedtuple("Point",['x','y'])
p = Point(11,2)
print(p.x)
print(p.y)
# dequeue
from collections import deque
q = deque(['a','b','c'])
print(q)
q.appendleft('d')
print(q)
- defaultdict
# defaultdict
from collections import defaultdict
func = lambda :"demo"
dd = defaultdict(func)
dd["one"] = 1
print(dd["rtwo"])
- Counter
from collections import Counter
c = Counter("111111111222222233333")
print(c)