2

So here is my problem, i have a dictionary with following key => values:

6bc51fb21fd9eefef4ec97a241733cd59b71e8e14ad70e9068d32002:политичка -> 2
6bc51fb21fd9eefef4ec97a241733cd59b71e8e14ad70e9068d32002:државата -> 2
6bc51fb21fd9eefef4ec97a241733cd59b71e8e14ad70e9068d32002:енергично -> 1
1caa60ebf9459d9cd406f1a03e1719b675dcfaad78292edc7e4a56be:полициска -> 1

I have this code to show the keys needed:

for key, value in count_db.iteritems():
        print key[:56]

So now i have:

6bc51fb21fd9eefef4ec97a241733cd59b71e8e14ad70e9068d32002 -> 2
6bc51fb21fd9eefef4ec97a241733cd59b71e8e14ad70e9068d32002 -> 2
6bc51fb21fd9eefef4ec97a241733cd59b71e8e14ad70e9068d32002 -> 1
1caa60ebf9459d9cd406f1a03e1719b675dcfaad78292edc7e4a56be -> 1

I need to merge them into:

6bc51fb21fd9eefef4ec97a241733cd59b71e8e14ad70e9068d32002 -> 5
1caa60ebf9459d9cd406f1a03e1719b675dcfaad78292edc7e4a56be -> 1

I have made this but i have not succeed in doing it correctly:

length_dic=len(count_db.keys())
for key, value in count_db.iteritems():
    count_element=key[:56]
    #print "%s => %s" % (key[:56], value) #value[:56]

    for i in range(length_dic):
        i+=1
        if count_element == key[:56]:
            itr+=int(value) 
        print i
    length_dic=length_dic-1

Any hints?

4 Answers 4

4

A trivial approach would be:

result = {}
for key, value in count_db.iteritems():
    result[key[:56]] = result.get(key[:56], 0) + value

You could also achieve the same with reduce if you want to get it on one line:

import collections
result = reduce(lambda x,y: x[y[0][:56]] += y[1] , count_db.iteritems(), collections.defaultdict(int))
Sign up to request clarification or add additional context in comments.

2 Comments

@DameJovanoski Every time you need to do some conversion or transformation on a dict or a list, there is usually a one-line solution, even though it might not look pretty ;)
When i try to print the list i get ValueError: too many values to unpack Any hints?
1

Given your dictionary as

>>> spam={"6bc51fb21fd9eefef4ec97a241733cd59b71e8e14ad70e9068d32002:AAAA": 2,
"6bc51fb21fd9eefef4ec97a241733cd59b71e8e14ad70e9068d32002:BBBB": 2,
"6bc51fb21fd9eefef4ec97a241733cd59b71e8e14ad70e9068d32002:CCCC": 1,
"1caa60ebf9459d9cd406f1a03e1719b675dcfaad78292edc7e4a56be:DDDD": 1
}

you can somewhat do like the following

>>> bacon=collections.defaultdict(int)
>>> for k,v in [(k[:56],v) for k,v in spam.iteritems()]:
    bacon[k]+=v


>>> bacon
defaultdict(<type 'int'>, {'6bc51fb21fd9eefef4ec97a241733cd59b71e8e14ad70e9068d32002': 5, '1caa60ebf9459d9cd406f1a03e1719b675dcfaad78292edc7e4a56be': 1})
>>> 

Comments

0

This is exactly what the Counter object (in version 2.7+) is for:

import collections
c = collections.Counter()

for key, value in count_db.iteritems():
    c[key[:56]] += value

1 Comment

ValueError: too many values to unpack when i want to print it
0

I didn't understand why you did all that in your code. I think this would do the job:

tmp_dict = {}
for key, value in count_db.iteritems():
    count_element=key[:56]

    if count_element in tmp_dict:
        tmp_dict[count_element] += value
    else:
        tmp_dict[count_element] = value

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.