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

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

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

问题

以下是已翻译的内容:

输入

list1 = ['Town1','Town2','Town3','Town4','Town5']

list2 = ['Town1','Town2','Town3','Town4','Town5']

输出

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

我尝试了

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

但结果中包括了('Town1', 'Town1')
英文:

Inputs:

list1 = ['Town1','Town2','Town3','Town4','Town5']

list2 = ['Town1','Town2','Town3','Town4','Town5']

Outputs:

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

I tried :

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

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

答案1

得分: 1

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

from itertools import product

list1 = ['Town1', 'Town2', 'Town3', 'Town4', 'Town5']

list2 = ['Town1', 'Town2', 'Town3', 'Town4', 'Town5']

comb = [pair for pair in product(list1, list2) if pair[0] != pair[1]]
print(comb)

输出结果是:

[('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:

from itertools import product

list1 = ['Town1','Town2','Town3','Town4','Town5']

list2 = ['Town1','Town2','Town3','Town4','Town5']

comb = [pair for pair in product(list1,list2) if pair[0] != pair[1]]
print(comb)

The output is:

[('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

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

result = []

for i in list1:
    for j in list2:
        if i != j:
            result.append((i, j))

print(result)
英文:
list1 = ['Town1','Town2','Town3','Town4','Town5']
list2 = ['Town1','Town2','Town3','Town4','Town5']

result = []

for i in list1:
    for j in list2:
        if i != j:
            result.append((i, j))

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:

确定