By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,773 Members | 1,244 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,773 IT Pros & Developers. It's quick & easy.

storing dictionaries into text file

P: 1
my dictionary looks like this

pri={2000:{0.233:0,0.444:0}}

i want to store it in below given format

2000 0.233 0.444

how to do that?? thanks in advance
Dec 6 '12 #1
Share this Question
Share on Google+
2 Replies

roberttonafelix
P: 16
sorry i am a python developer... i am not getting your question correctly... if you explain that it will be convenient to me to solve...
Dec 6 '12 #2

bvdet
Expert Mod 2.5K+
P: 2,851
Make an assignment to a str object:
Expand|Select|Wrap|Line Numbers
  1. >>> pri={2000:{0.233:0,0.444:0}}
  2. >>> s = "%s %s" % (pri.keys()[0], " ".join(map(str, sorted(pri[pri.keys()[0]]))))
  3. >>> s
  4. '2000 0.233 0.444'
  5. >>> 
In case pri has more than one key:
Expand|Select|Wrap|Line Numbers
  1. >>> pri={2000:{0.233:0,0.444:0}, 2001:{0.234:0,0.445:0}}
  2. >>> s = "\n".join([" ".join(map(str, [subkey]+sorted(pri[subkey].keys()))) for subkey in pri.keys()])
  3. >>> print s
  4. 2000 0.233 0.444
  5. 2001 0.234 0.445
  6. >>> 
Dec 6 '12 #3

Post your reply

Sign in to post your reply or Sign up for a free account.