多次迭代三个列表,在每次迭代中依次从列表中选取一个元素
Iterate through three lists multiple times sequentially picking one element from a list in each iteration
我想遍历三个列表中的每一个,在每次迭代中依次从每个列表中选取一个元素。
subjects=["Americans","Indians"]
verbs=["plays","watch"]
objects=["Baseball","cricket"]
for i,j,k in zip(subjects,verbs,objects):
print(i,j,k)
以上代码输出如下:
Americans plays Baseball
Indians watch cricket
但预期的输出是:
Americans play Baseball.
Americans play Cricket.
Americans watch Baseball.
Americans watch Cricket.
Indians play Baseball.
Indians play Cricket.
Indians watch Baseball.
Indians watch Cricket.
您可以这样迭代解决方案:
subjects=["Americans","Indians"]
verbs=["plays","watch"]
objects=["Baseball","cricket"]
for s in subjects:
for v in verbs:
for o in objects:
print(s,v,o+".")
使用itertools.product
得到所有可能的组合然后打印出来
>>> subjects=["Americans","Indians"]
>>> verbs=["plays","watch"]
>>> objects=["Baseball","cricket"]
>>>
>>> from itertools import product
>>> for x in product(subjects,verbs,objects):
... print('{} {} {}'.format(*x))
...
Americans plays Baseball
Americans plays cricket
Americans watch Baseball
Americans watch cricket
Indians plays Baseball
Indians plays cricket
Indians watch Baseball
Indians watch cricket
itertools.product
是最好的解决方案,但如果您不想导入模块,下一个最有效的解决方案是生成器表达式。
subjects = ["Americans", "Indians"]
verbs = ["play", "watch"]
objects = ["Baseball", "Cricket"]
# paragraph = "\n".join(f"{s} {v} {o}." for s in subjects for v in verbs for o in objects) # Alternative single-liner
for sentence in (f"{s} {v} {o}." for s in subjects for v in verbs for o in objects):
print(sentence)
输出
Americans play Baseball.
Americans play Cricket.
Americans watch Baseball.
Americans watch Cricket.
Indians play Baseball.
Indians play Cricket.
Indians watch Baseball.
Indians watch Cricket.
我想遍历三个列表中的每一个,在每次迭代中依次从每个列表中选取一个元素。
subjects=["Americans","Indians"]
verbs=["plays","watch"]
objects=["Baseball","cricket"]
for i,j,k in zip(subjects,verbs,objects):
print(i,j,k)
以上代码输出如下:
Americans plays Baseball
Indians watch cricket
但预期的输出是:
Americans play Baseball.
Americans play Cricket.
Americans watch Baseball.
Americans watch Cricket.
Indians play Baseball.
Indians play Cricket.
Indians watch Baseball.
Indians watch Cricket.
您可以这样迭代解决方案:
subjects=["Americans","Indians"]
verbs=["plays","watch"]
objects=["Baseball","cricket"]
for s in subjects:
for v in verbs:
for o in objects:
print(s,v,o+".")
使用itertools.product
得到所有可能的组合然后打印出来
>>> subjects=["Americans","Indians"]
>>> verbs=["plays","watch"]
>>> objects=["Baseball","cricket"]
>>>
>>> from itertools import product
>>> for x in product(subjects,verbs,objects):
... print('{} {} {}'.format(*x))
...
Americans plays Baseball
Americans plays cricket
Americans watch Baseball
Americans watch cricket
Indians plays Baseball
Indians plays cricket
Indians watch Baseball
Indians watch cricket
itertools.product
是最好的解决方案,但如果您不想导入模块,下一个最有效的解决方案是生成器表达式。
subjects = ["Americans", "Indians"]
verbs = ["play", "watch"]
objects = ["Baseball", "Cricket"]
# paragraph = "\n".join(f"{s} {v} {o}." for s in subjects for v in verbs for o in objects) # Alternative single-liner
for sentence in (f"{s} {v} {o}." for s in subjects for v in verbs for o in objects):
print(sentence)
输出
Americans play Baseball.
Americans play Cricket.
Americans watch Baseball.
Americans watch Cricket.
Indians play Baseball.
Indians play Cricket.
Indians watch Baseball.
Indians watch Cricket.