944,111 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 1761
  • Python RSS
Mar 24th, 2006
0

Problem extracting values of a Dictionary

Expand Post »
Hi! Thanks for your help in advance.....

Heres my case

I have a dictionary "d" with keys as string and a dictionary as integer "1" as keys to a list [1,2,3]

Python Syntax (Toggle Plain Text)
  1. d{"test.tif": {1: [1,2,3]}}
Now when I try looping through it........it gives me an error

Python Syntax (Toggle Plain Text)
  1. for i in d:
  2. for j in i:
  3. print j

it just prints test.tif

for some reason it just takes the key as a list of strings

Any solution to go arounf this problem?
Thanks
Shaf
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shafter111 is offline Offline
6 posts
since Feb 2006
Mar 25th, 2006
0

Re: Problem extracting values of a Dictionary

Hi!

Python Syntax (Toggle Plain Text)
  1. In [4]: for i in d:
  2. ...: print i
  3. ...:
  4. test.tif
So i is a string, and when you say for j in i: you iterate over that string Maybe you want one of the following:
Python Syntax (Toggle Plain Text)
  1. In [5]: for i in d:
  2. ...: for j in d[i]:
  3. ...: print j
  4. ...:
  5. 1
  6.  
  7. In [6]: for i in d:
  8. ...: for j in d[i]:
  9. ...: print d[i][j]
  10. ...:
  11. [1, 2, 3]
  12.  
  13. In [8]: for i in d:
  14. ...: for j in d[i]:
  15. ...: for k in d[i][j]:
  16. ...: print k
  17. ...:
  18. 1
  19. 2
  20. 3
Regards, mawe
Reputation Points: 19
Solved Threads: 58
Junior Poster
mawe is offline Offline
133 posts
since Sep 2005
Mar 25th, 2006
0

Re: Problem extracting values of a Dictionary

thanks ....but I figured it out.....

this is what I did.......

Python Syntax (Toggle Plain Text)
  1.  
  2. for l in j:
  3. z = j[l]
  4. for r in z:
  5. m = z[r]

now "m" has the inner list......

Thanks for the reply......but before on the second loop.....I did this

Python Syntax (Toggle Plain Text)
  1.  
  2. for l in j:
  3. for r in j[l]:
  4. for m in j[l[r]]:
  5. print m

the third for loop threw me off........I didnt know that I have to do it j[i][l]
format........thanks a lot
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shafter111 is offline Offline
6 posts
since Feb 2006
Mar 25th, 2006
0

Re: Problem extracting values of a Dictionary

There are also some dictionary functions that can give you access to the inner sanctum:
Python Syntax (Toggle Plain Text)
  1. d = {"test.tif": {1: [1,2,3]}}
  2. for key, dic in d.iteritems():
  3. for x in dic.values()[0]:
  4. print x
  5. """
  6. result =
  7. 1
  8. 2
  9. 3
  10. """
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Mar 27th, 2006
0

Re: Problem extracting values of a Dictionary

Nice! This looks much better........Thanks for all the help guys!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shafter111 is offline Offline
6 posts
since Feb 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Weird problem with float or double....
Next Thread in Python Forum Timeline: Help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC