Problem extracting values of a Dictionary

Thread Solved

Join Date: Feb 2006
Posts: 6
Reputation: shafter111 is an unknown quantity at this point 
Solved Threads: 0
shafter111 shafter111 is offline Offline
Newbie Poster

Problem extracting values of a Dictionary

 
0
  #1
Mar 24th, 2006
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]

  1. d{"test.tif": {1: [1,2,3]}}
Now when I try looping through it........it gives me an error

  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
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 133
Reputation: mawe is an unknown quantity at this point 
Solved Threads: 58
mawe mawe is offline Offline
Junior Poster

Re: Problem extracting values of a Dictionary

 
0
  #2
Mar 25th, 2006
Hi!

  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:
  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
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 6
Reputation: shafter111 is an unknown quantity at this point 
Solved Threads: 0
shafter111 shafter111 is offline Offline
Newbie Poster

Re: Problem extracting values of a Dictionary

 
0
  #3
Mar 25th, 2006
thanks ....but I figured it out.....

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

  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

  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
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,024
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 932
Moderator
vegaseat's Avatar
vegaseat vegaseat is online now Online
DaniWeb's Hypocrite

Re: Problem extracting values of a Dictionary

 
0
  #4
Mar 25th, 2006
There are also some dictionary functions that can give you access to the inner sanctum:
  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. """
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 6
Reputation: shafter111 is an unknown quantity at this point 
Solved Threads: 0
shafter111 shafter111 is offline Offline
Newbie Poster

Re: Problem extracting values of a Dictionary

 
0
  #5
Mar 27th, 2006
Nice! This looks much better........Thanks for all the help guys!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC