我的清单是:
groupA=['Russia', 'Egypt', 'Saudi Arabia', 'Uruguay']
因此,我想打印出将互相比赛的所有独特的球队组合:
Russia Vs. Egypt
Russia Vs. Saudi Arabia
Russia Vs. Uruguay
Egypt Vs. Saudi Arabia
Egypt Vs. Uruguay
Saudi Arabia Vs. Uruguay
我可以使用for循环吗?
解决方法:
这应该做您想要的:
groupA=['Russia', 'Egypt', 'Saudi Arabia', 'Uruguay']
for i in range(len(groupA)):
for j in range(i+1, len(groupA)):
print("{} Vs. {}".format(groupA[i], groupA[j]))
如果您更喜欢使用itertools:
from itertools import combinations
groupA=['Russia', 'Egypt', 'Saudi Arabia', 'Uruguay']
for combo in combinations(groupA, 2):
print("{} Vs. {}".format(combo[0], combo[1]))