首页 文章

比较Python中的计数器列表

提问于
浏览
4

我在python中有一个计数器列表:

[Counter({12.011: 30.0, 15.999: 2.0}),
Counter({12.011: 12.0, 15.999: 2.0}),... Counter({12.011: 40.0, 
15.999: 5.0, 79.904: 5.0})]

如何查找每个Counter元素的计数 . 计数器给我一个不可用类型的错误 . 我想的另一种方法是在嵌套的for循环中遍历列表,检查每个计数器是否等于列表中的任何其他计数器并相应地增加字典 . 有更好的解决方案吗?

2 回答

  • 2

    在Python <= 3.6中,你最好的选择可能是将每个 Counter 与其他 Counter 进行比较 .

    在Python> = 3.7中有更好的方法 . 因为 Counterdict 的子类,并且 dict 现在保持其顺序,所以可以使用每个 Counter 对象的字符串表示作为其哈希 .

    这有点hacky但它的工作原理:

    from collections import Counter
    
    li = [Counter({'a': 1, 'b': 2, 'c': 3}),
          Counter({'a': 1, 'b': 2, 'c': 4}),
          Counter({'a': 1, 'b': 3, 'c': 3}),
          Counter({'a': 1, 'b': 2, 'c': 3})]
    
    
    Counter.__hash__ = lambda counter: hash(str(counter))
    
    print(Counter(li))
    # Counter({Counter({'c': 3, 'b': 2, 'a': 1}): 2,
    #          Counter({'c': 4, 'b': 2, 'a': 1}): 1,
    #          Counter({'b': 3, 'c': 3, 'a': 1}): 1})
    
  • 2

    您可以将每个Counter转换为key,value(items)元组的冻结集,然后将其用作传递给Counter的元素(因为frozenset是可清除的),例如:

    import random
    
    from collections import Counter
    
    random.seed(42)
    
    counts = [Counter([random.randint(0, 2) for _ in range(10)]) for _ in range(10)]
    
    uniques = {frozenset(e.items()): e for e in counts}
    
    counters_count = Counter(map(lambda e : frozenset(e.items()), counts))
    
    for key, count in counters_count.items():
        print uniques[key], count
    

    Output

    Counter({0: 7, 1: 2, 2: 1}) 1
    Counter({1: 4, 2: 4, 0: 2}) 1
    Counter({0: 4, 1: 4, 2: 2}) 2
    Counter({0: 4, 1: 3, 2: 3}) 1
    Counter({0: 5, 2: 3, 1: 2}) 1
    Counter({1: 5, 2: 5}) 1
    Counter({0: 5, 1: 4, 2: 1}) 1
    Counter({0: 4, 2: 4, 1: 2}) 1
    Counter({2: 4, 0: 3, 1: 3}) 1
    

相关问题