如何创建两个列表的唯一组合,每个组合中的元素都不相同?Python

huangapple go评论87阅读模式
英文:

Given two lists, how to create a unique combinations and the elements in each combination are different? Python

问题

以下是已翻译的内容:

  1. 输入
  2. list1 = ['Town1','Town2','Town3','Town4','Town5']
  3. list2 = ['Town1','Town2','Town3','Town4','Town5']
  4. 输出
  5. [('Town1', 'Town2'), ('Town3', 'Town4'), ('Town4', 'Town4'), ('Town4', 'Town5'), ('Town1', 'Town4'), ('Town3', 'Town5'), ('Town1', 'Town3'), ('Town2', 'Town4'), ('Town1', 'Town5'), ('Town2', 'Town3'), ('Town2', 'Town5')]
  6. 我尝试了
  7. comb = list(product(list1, list2))
  8. comb1 = [sorted(item) for item in comb]
  9. comb2 = list(set(map(tuple, comb1))
  10. 但结果中包括了('Town1', 'Town1')
英文:

Inputs:

  1. list1 = ['Town1','Town2','Town3','Town4','Town5']
  2. list2 = ['Town1','Town2','Town3','Town4','Town5']

Outputs:

  1. [('Town1', 'Town2'), ('Town3', 'Town4'), ('Town4', 'Town4'), ('Town4', 'Town5'), ('Town1', 'Town4'), ('Town3', 'Town5'), ('Town1', 'Town3'), ('Town2', 'Town4'), ('Town1', 'Town5'), ('Town2', 'Town3'), ('Town2', 'Town5')]

I tried :

  1. comb = list(product(list1,list2))
  2. comb1 = [sorted(item) for item in comb]
  3. comb2 = list(set(map(tuple,comb1)))

But ('Town1', 'Town1') is involved in the result.

答案1

得分: 1

你可以使用一个列表推导式来排除具有重复元素的配对:

  1. from itertools import product
  2. list1 = ['Town1', 'Town2', 'Town3', 'Town4', 'Town5']
  3. list2 = ['Town1', 'Town2', 'Town3', 'Town4', 'Town5']
  4. comb = [pair for pair in product(list1, list2) if pair[0] != pair[1]]
  5. print(comb)

输出结果是:

  1. [('Town1', 'Town2'), ('Town1', 'Town3'), ('Town1', 'Town4'), ('Town1', 'Town5'), ('Town2', 'Town1'), ('Town2', 'Town3'), ('Town2', 'Town4'), ('Town2', 'Town5'), ('Town3', 'Town1'), ('Town3', 'Town2'), ('Town3', 'Town4'), ('Town3', 'Town5'), ('Town4', 'Town1'), ('Town4', 'Town2'), ('Town4', 'Town3'), ('Town4', 'Town5'), ('Town5', 'Town1'), ('Town5', 'Town2'), ('Town5', 'Town3'), ('Town5', 'Town4')]
英文:

You can exclude the pairs with repeated elements with a list comprehension:

  1. from itertools import product
  2. list1 = ['Town1','Town2','Town3','Town4','Town5']
  3. list2 = ['Town1','Town2','Town3','Town4','Town5']
  4. comb = [pair for pair in product(list1,list2) if pair[0] != pair[1]]
  5. print(comb)

The output is:

  1. [('Town1', 'Town2'), ('Town1', 'Town3'), ('Town1', 'Town4'), ('Town1', 'Town5'), ('Town2', 'Town1'), ('Town2', 'Town3'), ('Town2', 'Town4'), ('Town2', 'Town5'), ('Town3', 'Town1'), ('Town3', 'Town2'), ('Town3', 'Town4'), ('Town3', 'Town5'), ('Town4', 'Town1'), ('Town4', 'Town2'), ('Town4', 'Town3'), ('Town4', 'Town5'), ('Town5', 'Town1'), ('Town5', 'Town2'), ('Town5', 'Town3'), ('Town5', 'Town4')]

答案2

得分: 0

  1. list1 = ['Town1', 'Town2', 'Town3', 'Town4', 'Town5']
  2. list2 = ['Town1', 'Town2', 'Town3', 'Town4', 'Town5']
  3. result = []
  4. for i in list1:
  5. for j in list2:
  6. if i != j:
  7. result.append((i, j))
  8. print(result)
英文:
  1. list1 = ['Town1','Town2','Town3','Town4','Town5']
  2. list2 = ['Town1','Town2','Town3','Town4','Town5']
  3. result = []
  4. for i in list1:
  5. for j in list2:
  6. if i != j:
  7. result.append((i, j))
  8. print(result)

huangapple
  • 本文由 发表于 2023年2月6日 04:11:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75355199.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定