1

What is correct way to convert Python dict in single string?

I have example program with code:

keywordsList = {u'[email protected]': 2, u'http://www.3ho.de/': 4, u'http://www.kundalini-yoga-zentrum-berlin.de/index.html': 1, u'[email protected]': 3}

keywordsCount = sorted(keywordsList.items(),key=lambda(k,v):(v,k),reverse=True)

for words in keywordsCount:
    print words[0], " - count: ", words[1]

so after I sort my items I get result like this:

http://www.3ho.de/  - count:  4
[email protected]  - count:  3
[email protected]  - count:  2
http://www.kundalini-yoga-zentrum-berlin.de/index.html  - count:  1

And my question is what is correct way to combine all dict stuff with cout in one single string that would look printed out something like:

'http://www.3ho.de/ : 4, [email protected] : 3, [email protected] : 2, http://www.kundalini-yoga-zentrum-berlin.de/index.html : 1'

or something similar but in logic way?

4
  • Is the sorting relevant? Do you want your output string to be sorted? Commented Aug 14, 2013 at 13:06
  • str(keywordsList) if you only want it as a string, and no modification to the actual dictionary? Commented Aug 14, 2013 at 13:06
  • yea the sorting is relevant for me, that is the reason why I don't doing that @Torxed suggestion Commented Aug 14, 2013 at 13:07
  • possible duplicate of Convert Python dict to object Commented Aug 14, 2013 at 13:08

3 Answers 3

2

Use str.join() judiciously:

', '.join([' : '.join((k, str(keywordsList[k]))) for k in sorted(keywordsList, key=keywordsList. get, reverse=True)])

Outputs:

>>> ', '.join([' : '.join((k, str(keywordsList[k]))) for k in sorted(keywordsList, key=keywordsList. get, reverse=True)])
u'http://www.3ho.de/ : 4, [email protected] : 3, [email protected] : 2, http://www.kundalini-yoga-zentrum-berlin.de/index.html : 1'

You should really look into collections.Counter() and save yourself having to sort like that. The Counter.most_common() method lists items in reverse sorted order by frequency for you.

Sign up to request clarification or add additional context in comments.

6 Comments

You can also drop the list you build before joining on the comma ', '.join()
@NoelEvans: Using a list comprehension is more efficient than a generator expression when using str.join(). See [list comprehension without [ ], Python](list comprehension without [ ], Python)
This will work for me! Thank you, will accept your answer ASAP
@Martijn Pieters that's presumably when the size of the list you're joining on it very small. When larger, it'd be better to use a generator?
@NoelEvans: No; the extra cost is in how the list object is created. The str.join() method requires a sequence (it'll iterate twice over the input) so a generator is converted to a list first. That is always going to be slower than creating list to start with with a list comprehension.
|
0

I'm not sure about the correct way but you could also do this:

', '.join(' : '.join([a, str(b)]) for a, b in keywordsCount)

1 Comment

This still requires the keywordsCount list to be constructed.
0

use conventional methods,which are simple .This worked for me

dict = {'Alice': '2341', 'Beth': '9102', 'Cecil': '3258'}
d=" " 
for  i in dict:
 a=i
 b=dict[i]
 c=i+":"+dict[i]
 d=d+c+'\n'

print d 

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.