统计列表中的配对数,不考虑顺序。

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

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)]

huangapple
  • 本文由 发表于 2020年1月3日 17:57:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/59576440.html
匿名

发表评论

匿名网友

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

确定