【问题标题】:Count how many times a tuple appears in ouput results计算一个元组在输出结果中出现的次数
【发布时间】:2016-05-15 04:34:52
【问题描述】:

我想计算一个元组在我的输出“结果”中出现了多少次(我改进了我之前的问题 How to join many “listed” tuples into one tuple in Python?)。

所以我这样做了:

from collections import Counter
liste = [1,2,3,5,10]
liste2 = [[1,2,3,5,10], [1,2], [1,5,10], [3,5,10], [1,2,5,10]]
for elt in liste2:
    syn = elt # identify each sublist of liste2 as syn
    nTuple = len(syn)   # number of elements in the syn
    for i in liste:
        myTuple = ()
        if syn.count(i): # check if an item of liste is in liste2
               myTuple = (i, nTuple)
               if len(myTuple) == '0': # remove the empty tuples
                  del(myTuple)
        else:
            result = [myTuple] 
            c = Counter(result)
            for item in c.items():
                print(item)

我得到了这些结果:

((1, 5), 1)

((2, 5), 1)

((3, 5), 1)

((5, 5), 1)

((10, 5), 1)

((1, 2), 1)

((2, 2), 1)

((1, 3), 1)

((5, 3), 1)

((10, 3), 1)

((3, 3), 1)

((5, 3), 1)

((10, 3), 1)

((1, 4), 1)

((2, 4), 1)

((5, 4), 1)

((10, 4), 1)

而不是让一些 elts N 次(例如 ((5, 3), 1)((10, 3), 1) 出现两次),我想要一个元组(键,值),其中值 = 键出现在“结果”中的次数。

我想得到这样的“结果”:

((1, 5), 1)

((2, 5), 1)

((3, 5), 1)

((5, 5), 1)

((10, 5), 1)

((1, 2), 1)

((2, 2), 1)

((1, 3), 1)

((5, 3), 2)

((10, 3), 2)

((3, 3), 1)

((1, 4), 1)

((2, 4), 1)

((5, 4), 1)

((10, 4), 1)

谢谢

【问题讨论】:

  • Counter 应该已经这样做了,那么它们实际上是整数元组还是其他东西?
  • 对不起,这是一个错误。请阅读'syn'
  • 如果 syn.count 是一种查看列表中是否有内容的糟糕方法,而len(myTuple) 永远不会等于字符串

标签: python list


【解决方案1】:

我相信问题出在:

c = Counter(result)

每次迭代,你都会创建一个新的计数器,如果你在开始时实例化一个计数器,然后输出它的元素,我希望你会得到你需要的输出:

from collections import Counter
liste = [1,2,3,5,10]
liste2 = [[1,2,3,5,10], [1,2], [1,5,10], [3,5,10], [1,2,5,10]]
c = Counter()
for elt in liste2:
    syn = elt # identify each sublist of liste2 as syn
    nTuple = len(syn)   # number of elements in the syn
    for i in liste:
        myTuple = ()
        if syn.count(i): # check if an item of liste is in liste2
               myTuple = (i, nTuple)
               if len(myTuple) == 0: # remove the empty tuples
                  del(myTuple)
               else:
                   result = myTuple
                   c[result] += 1
for item in c.items():
    print(item)

另外一个需要做的改变是你把东西放在柜台的什么地方,以及你在里面放了什么。见上面的代码。

已编辑解决方案以回答您的问题。

编辑,最初甚至没有注意到 if 语句中的错误。如果 len(myTuple)==0 实际上是多余的,因为不会在其中创建空元组。您的示例中有几条多余的行。以下代码将完全一样:

from collections import Counter
liste = [1,2,3,5,10]
liste2 = [[1,2,3,5,10], [1,2], [1,5,10], [3,5,10], [1,2,5,10]]
c = Counter()
for elt in liste2:
    nTuple = len(elt)   # number of elements in the syn
    for i in liste:
        if elt.count(i): 
               myTuple = (i, nTuple)
               c[myTuple] += 1
for item in c.items():
    print(item)

就像我在评论中所说的,您可以通过将它封装在一个函数中来让它干净地运行:

>>> from collections import Counter
... liste = [1,2,3,5,10]
... liste2 = [[1,2,3,5,10], [1,2], [1,5,10], [3,5,10], [1,2,5,10]]
... def get_syncounts(liste, liste2):
...     c = Counter()
...     for elt in liste2:
...         nTuple = len(elt)   # number of elements in the syn
...         for i in liste:
...             if elt.count(i): 
...                    myTuple = (i, nTuple)
...                    c[myTuple] += 1
...     for item in c.items():
...         print(item)
>>> get_syncounts(liste, liste2)
((1, 2), 1)
((10, 5), 1)
((1, 3), 1)
((5, 5), 1)
((5, 4), 1)
((1, 4), 1)
((1, 5), 1)
((10, 4), 1)
((2, 2), 1)
((3, 3), 1)
((2, 5), 1)
((5, 3), 2)
((10, 3), 2)
((2, 4), 1)
((3, 5), 1)
>>> get_syncounts(liste, liste2)
((1, 2), 1)
((10, 5), 1)
((1, 3), 1)
((5, 5), 1)
((5, 4), 1)
((1, 4), 1)
((1, 5), 1)
((10, 4), 1)
((2, 2), 1)
((3, 3), 1)
((2, 5), 1)
((5, 3), 2)
((10, 3), 2)
((2, 4), 1)
((3, 5), 1)
>>> 

【讨论】:

  • 感恩米勒!这正是我一直在寻找的。你拯救了我的一天。
  • 非常欢迎。接受我的回答表示感谢:D
  • 然后也许将整个事情封装在一个函数中,这样每次你想运行它时计数都是新鲜的
  • 我在上面添加了一些关于运行的 cmets。我想避免每次运行程序时值都会增加
  • 放弃了您的编辑并添加了您应该如何将其放入函数中并重复您心中的内容
【解决方案2】:

您可以每次将结果附加到列表中,并且在附加到列表之前,您可以检查该元素是否存在于列表中。 这样可以避免重复。

    store_results = [] 
    if result not in store_result:
             store_result.append(result)

然后一个列表很容易转换成一个元组

print tuple(store_results)

【讨论】:

    猜你喜欢
    • 2019-06-05
    • 2016-09-23
    • 1970-01-01
    • 1970-01-01
    • 2022-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-07
    相关资源
    最近更新 更多