制作与世界杯相关的程序

时间:2019-09-06 23:08:39

标签: python python-3.x

我只想打印所有参加2019年世界杯的球队,为此我编写了一个简单的程序,但其中包含一些问题,问题是,一对球员会重复1次(通过打印反之亦然),就像-

印度澳大利亚 ----在其他一些双印之后- 澳大利亚印度

此程序中还包含一个列表,但此网站未显示

team = ["INDIA", "AUSTRALIA", "ENGLAND", "PAKISTAN", "SOUTH AFRICA", "AFGHANISTAN", "WEST INDIES", "SRI LANKA", "NEW ZEALAND", "BANGLADESH"]
team1 = ["INDIA", "AUSTRALIA", "ENGLAND", "PAKISTAN", "SOUTH AFRICA", "AFGHANISTAN", "WEST INDIES", "SRI LANKA", "NEW ZEALAND", "BANGLADESH"]
print(team)

for x in team:
  for y in team1:
    if x == y:
      continue
    print(x, y)

2 个答案:

答案 0 :(得分:2)

随着外循环的每次新迭代,内循环从列表的开头开始,因此重复对,可以改为按索引循环,并避免重复排列的对和两个元素相等的对:

team1 = ["INDIA", "AUSTRALIA", "ENGLAND", "PAKISTAN", "SOUTH AFRICA", "AFGHANISTAN", "WEST INDIES", "SRI LANKA", "NEW ZEALAND", "BANGLADESH"]

for i in range(len(team1) - 1):
  for j in range(i + 1, len(team1)):
    print(team1[i], team1[j])

输出:

INDIA AUSTRALIA
INDIA ENGLAND
INDIA PAKISTAN
INDIA SOUTH AFRICA
INDIA AFGHANISTAN
INDIA WEST INDIES
INDIA SRI LANKA
INDIA NEW ZEALAND
INDIA BANGLADESH
AUSTRALIA ENGLAND
AUSTRALIA PAKISTAN
AUSTRALIA SOUTH AFRICA
AUSTRALIA AFGHANISTAN
AUSTRALIA WEST INDIES
AUSTRALIA SRI LANKA
AUSTRALIA NEW ZEALAND
AUSTRALIA BANGLADESH
ENGLAND PAKISTAN
ENGLAND SOUTH AFRICA
ENGLAND AFGHANISTAN
ENGLAND WEST INDIES
ENGLAND SRI LANKA
ENGLAND NEW ZEALAND
ENGLAND BANGLADESH
PAKISTAN SOUTH AFRICA
PAKISTAN AFGHANISTAN
PAKISTAN WEST INDIES
PAKISTAN SRI LANKA
PAKISTAN NEW ZEALAND
PAKISTAN BANGLADESH
SOUTH AFRICA AFGHANISTAN
SOUTH AFRICA WEST INDIES
SOUTH AFRICA SRI LANKA
SOUTH AFRICA NEW ZEALAND
SOUTH AFRICA BANGLADESH
AFGHANISTAN WEST INDIES
AFGHANISTAN SRI LANKA
AFGHANISTAN NEW ZEALAND
AFGHANISTAN BANGLADESH
WEST INDIES SRI LANKA
WEST INDIES NEW ZEALAND
WEST INDIES BANGLADESH
SRI LANKA NEW ZEALAND
SRI LANKA BANGLADESH
NEW ZEALAND BANGLADESH

答案 1 :(得分:0)

您可以使用real打印两个的所有组合。

itertools.combinations()