英文:
Count number of pairs in list disregarding order
问题
你可以调整你的代码以获得以下输出:
import collections
lst = [['a','b'],['b','a'],['c','d'],['c','d'],['d','c']]
counts = collections.Counter(tuple(sorted(pair)) for pair in lst)
result = [(a, b, v) for (a, b), v in counts.items()]
print(result)
这将输出:
[('a', 'b', 2), ('c', 'd', 3)]
英文:
In example, if I have the following script:
import collections
lst = [['a','b'],['b','a'],['c','d'],['c','d'],['d','c']]
print([(a, b, v) for (a, b),v in collections.Counter(map(tuple,lst)).items()])
I get as output:
[('a', 'b', 1), ('b', 'a', 1), ('c', 'd', 2), ('d', 'c', 1)]
Can I adapt my code to yield the following output:
[('a', 'b', 2), ('c', 'd', 3)]
So a function that doesn't include the order of the pairs?
答案1
得分: 4
使用一个不关心顺序的数据结构。在这种情况下,您需要使用frozenset
而不是普通的set
,因为Counter
需要它是可哈希的。但基本上,这只是将您原始代码中的tuple
替换为frozenset
:
print([(a, b, v) for (a, b), v in collections.Counter(map(frozenset, lst)).items()])
输出:
[('a', 'b', 2), ('d', 'c', 3)]
英文:
Use a data structure that doesn't care about order. In this case you'll need frozenset
instead of a regular set
because Counter
requires it to be hashable. But basically it's a simple substitution of tuple
in your original code for frozenset
:
print([(a, b, v) for (a, b),v in collections.Counter(map(frozenset,lst)).items()])
Output:
[('a', 'b', 2), ('d', 'c', 3)]
答案2
得分: 2
你可以在计数之前对列表中的每个元素进行排序,像这样:
import collections
lst = [['a', 'b'], ['b', 'a'], ['c', 'd'], ['c', 'd'], ['d', 'c']]
sorted_lst = [sorted(x) for x in lst]
print([(a, b, v) for (a, b), v in collections.Counter(map(tuple, sorted_lst)).items()])
输出:
[('a', 'b', 2), ('c', 'd', 3)]
英文:
You could just sort each element in the list before counting, like so:
import collections
lst = [['a','b'],['b','a'],['c','d'],['c','d'],['d','c']]
sorted_lst = [sorted(x) for x in lst]
print([(a, b, v) for (a, b),v in collections.Counter(map(tuple,sorted_lst)).items()])
Output:
[('a', 'b', 2), ('c', 'd', 3)]
答案3
得分: 2
将列表在获取其集合之前进行排序可以解决这个问题。
import collections
lst = [['a', 'b'], ['b', 'a'], ['c', 'd'], ['c', 'd'], ['d', 'c']]
sort_list = [sorted(x) for x in lst]
print([(a, b, v) for (a, b), v in collections.Counter(map(tuple, sort_list)).items()])
英文:
Sorting the list before you get collections of it solves the problem.
import collections
lst = [['a','b'],['b','a'],['c','d'],['c','d'],['d','c']]
sort_list = sorted(x) for x in lst
print([(a, b, v) for (a, b),v in collections.Counter(map(tuple,sort_list)).items()])
答案4
得分: 1
You could sort the values of the key a,b
and use groupby
in itertools
and then sum
all the elements in the group.
import itertools as it
lst = [['a','b'],['b','a'],['c','d'],['c','d'],['d','c']]
output = [(*group,sum(1 for i in elements)) for group,elements in it.groupby(lst,key=lambda x:sorted(x))]
print(output)
OUTPUT
[('a', 'b', 2), ('c', 'd', 3)]
英文:
You could sort the values of the key a,b
and use groupby
in itertools
and then sum
all the elements in the group.
import itertools as it
lst = [['a','b'],['b','a'],['c','d'],['c','d'],['d','c']]
output = [(*group,sum(1 for i in elements)) for group,elements in it.groupby(lst,key=lambda x:sorted(x))]
print(output)
OUTPUT
[('a', 'b', 2), ('c', 'd', 3)]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论