多个 for 循环迭代器解压 Python
Multiple for loop iterators to unpack in Python
我想知道是否可以同时遍历两个列表。
类似的东西:
for x, m in list1, list2:
...
我知道我应该使用“.items()”,但我不想从两个列表创建字典。
有什么想法吗?
使用zip.
for x, m in zip(list1, list2):
zip(*iterables)
Make an iterator that aggregates elements from each of the iterables.
Returns an iterator of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables. The iterator stops when the shortest input iterable is exhausted. With a single iterable argument, it returns an iterator of 1-tuples. With no arguments, it returns an empty iterator.
我想知道是否可以同时遍历两个列表。
类似的东西:
for x, m in list1, list2:
...
我知道我应该使用“.items()”,但我不想从两个列表创建字典。
有什么想法吗?
使用zip.
for x, m in zip(list1, list2):
zip(*iterables)
Make an iterator that aggregates elements from each of the iterables.
Returns an iterator of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables. The iterator stops when the shortest input iterable is exhausted. With a single iterable argument, it returns an iterator of 1-tuples. With no arguments, it returns an empty iterator.