-4

I want to count how many times a tuple appears in my ouput 'result' (I refine my former question How to join many “listed” tuples into one tuple in Python?).

So I did this:

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)

and I got these results:

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

Instead of having some elts N times (e.g ((5, 3), 1) and ((10, 3), 1) appear twice), I would like to have a tuple(key,value) where value = the number of times key appears in 'result'.

I would like to get 'result' like this:

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

Thanks

Community
  • 1
  • 1
dcpula
  • 29
  • 6

2 Answers2

1

I believe the problem is on the line:

c = Counter(result)

Each iteration, you're creating a new Counter, if you instead instantiate a counter at the start, then output it's elements AFTER the fact, I expect you would get the output you need:

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)

An additional change needed was WHERE you put things in the counter, and what you put in it. see above code.

Have edited the solution so it answers your question.

Edit, didn't even notice the error in your if statement initially. if len(myTuple)==0 is actually redundant as no empty tuples will be created in this. There are several redundant lines in your example. The following code will do exactly the same:

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)

Like I said in my comment, you can get this to run cleanly by encapsulating it in a function:

>>> 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)
>>> 
greg_data
  • 2,247
  • 13
  • 20
0

you can append the result each time in a list and before appending to the list you can check if the element is present in the list or not. that way you can avoid having duplicates.

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

and then a list is easily convertible into a tuple

print tuple(store_results)
sumit
  • 3,210
  • 1
  • 19
  • 37