1

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

I have example program with code:

keywordsList = {u'roland.luethe1@gmail.com': 2, u'http://www.3ho.de/': 4, u'http://www.kundalini-yoga-zentrum-berlin.de/index.html': 1, u'ergo@ananda-pur.de': 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
ergo@ananda-pur.de  - count:  3
roland.luethe1@gmail.com  - 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, ergo@ananda-pur.de : 3, roland.luethe1@gmail.com : 2, http://www.kundalini-yoga-zentrum-berlin.de/index.html : 1'

or something similar but in logic way?

dzordz
  • 2,277
  • 13
  • 51
  • 74
  • Is the sorting relevant? Do you want your output string to be sorted? – Brionius Aug 14 '13 at 13:06
  • `str(keywordsList)` if you only want it as a string, and no modification to the actual dictionary? – Torxed Aug 14 '13 at 13:06
  • yea the sorting is relevant for me, that is the reason why I don't doing that @Torxed suggestion – dzordz Aug 14 '13 at 13:07
  • possible duplicate of [Convert Python dict to object](http://stackoverflow.com/questions/1305532/convert-python-dict-to-object) – PW Kad Aug 14 '13 at 13:08

3 Answers3

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, ergo@ananda-pur.de : 3, roland.luethe1@gmail.com : 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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • You can also drop the list you build before joining on the comma ', '.join() – Noel Evans Aug 14 '13 at 13:10
  • 1
    @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](http://stackoverflow.com/q/9060653)) – Martijn Pieters Aug 14 '13 at 13:11
  • This will work for me! Thank you, will accept your answer ASAP – dzordz Aug 14 '13 at 13:12
  • @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? – Noel Evans Aug 14 '13 at 13:18
  • 1
    @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. – Martijn Pieters Aug 14 '13 at 13:20
  • @NoelEvans: A larger input actually makes that worse. – Martijn Pieters Aug 14 '13 at 13:20
  • @Martijn Pieters Wow. Interesting. Thank you – Noel Evans Aug 14 '13 at 13:21
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)
Noel Evans
  • 8,113
  • 8
  • 48
  • 58
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 
user2492854
  • 401
  • 2
  • 12
  • 32