Count how many times a tuple appears in ouput results
我想计算一个元组在我的输出"result"中出现了多少次(我改进了以前的问题,如何在python中将许多"列出的"元组连接成一个元组?)。
所以我这样做了:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | 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)
号
我希望有一个元组(key,value),其中value=key在'result'中出现的次数,而不是有一些elt 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)
号
谢谢
我相信问题就在网上:
1 | c = Counter(result) |
每次迭代,您都在创建一个新的计数器,如果您在开始时实例化一个计数器,然后在事实之后输出它的元素,我希望您能够得到所需的输出:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 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实际上是多余的,因为不会在此创建空的tuple。您的示例中有几个多余的行。以下代码的作用完全相同:
1 2 3 4 5 6 7 8 9 10 11 12 | 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) |
正如我在我的评论中所说,您可以通过将其封装在函数中使其干净地运行:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | >>> 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) >>> |
。
您可以在列表中每次追加结果,在追加到列表之前,您可以检查元素是否存在于列表中。这样可以避免重复。
1 2 3 | store_results = [] if result not in store_result: store_result.append(result) |
然后列表很容易转换成元组
1 | print tuple(store_results) |
。