9 Replies - 798 Views - Last Post: 17 September 2012 - 08:35 AM Rate Topic: -----

#1 NunoSantos  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 04-September 12

sort dictionary

Posted 12 September 2012 - 08:42 AM

Hi guys...i have one doubt, can, anybody, give me an hand?
i have one dictionary that contain keys. These keys are taxonomics levels but they are unsorted.
How can i sort this taxonomic levels by these order: domain, superkingdom, kingdom, phylum, class,order, family, genus, species?
for key in dtResult:
            print "\nPresenting results for: " + str(key).upper()
            for key2 in dtResult[key]:
                print "\t" + str(key2) + ": " +str(dtResult[key][key2]) 


that code give me what i want but not in the order that i specified before.
ideas?
thanks
Nuno

This post has been edited by macosxnerd101: 12 September 2012 - 08:45 AM
Reason for edit:: Fixed code tags


Is This A Good Question/Topic? 0
  • +

Replies To: sort dictionary

#2 Tayacan  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 145
  • View blog
  • Posts: 275
  • Joined: 18-January 11

Re: sort dictionary

Posted 12 September 2012 - 10:17 AM

A dictionary is unsorted by definition - the keys are the indices. What you can do is extract a list of keys, sort it, and then look up the items in the dictionary in that order. Look here.
Was This Post Helpful? 2
  • +
  • -

#3 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: sort dictionary

Posted 12 September 2012 - 12:17 PM

View PostTayacan, on 12 September 2012 - 01:17 PM, said:

A dictionary is unsorted by definition - the keys are the indices. What you can do is extract a list of keys, sort it, and then look up the items in the dictionary in that order. Look here.

Well said.
Was This Post Helpful? 0
  • +
  • -

#4 charles314  Icon User is offline

  • New D.I.C Head

Reputation: -11
  • View blog
  • Posts: 18
  • Joined: 13-September 12

Re: sort dictionary

Posted 13 September 2012 - 02:12 PM

d1=
d1={'a domain':111, 'b superkingdom': 222, 'c kingdom': 333, 'd phylum': 444, 'e class': 555, 'f order': 666, 'g family': 777, 'h genus': 888, 'i species': 999}

<br/>
<br/>
sorted(d1)
['a domain', 'b superkingdom', 'c kingdom', 'd phylum', 'e class', 'f order', 'g family', 'h genus', 'i species']

Was This Post Helpful? 0
  • +
  • -

#5 charles314  Icon User is offline

  • New D.I.C Head

Reputation: -11
  • View blog
  • Posts: 18
  • Joined: 13-September 12

Re: sort dictionary

Posted 13 September 2012 - 02:34 PM

Sorry about the post. Not sure what happened, and, no way to edit I guess.
Was This Post Helpful? 0
  • +
  • -

#6 Tayacan  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 145
  • View blog
  • Posts: 275
  • Joined: 18-January 11

Re: sort dictionary

Posted 14 September 2012 - 01:12 AM

View Postcharles314, on 13 September 2012 - 09:12 PM, said:

d1=
d1={'a domain':111, 'b superkingdom': 222, 'c kingdom': 333, 'd phylum': 444, 'e class': 555, 'f order': 666, 'g family': 777, 'h genus': 888, 'i species': 999}

<br/>
<br/>
sorted(d1)
['a domain', 'b superkingdom', 'c kingdom', 'd phylum', 'e class', 'f order', 'g family', 'h genus', 'i species']


Notice how you only get the keys this way - you'd then use this nice, sorted list of keys to look up stuff in the right order.
Was This Post Helpful? 0
  • +
  • -

#7 charles314  Icon User is offline

  • New D.I.C Head

Reputation: -11
  • View blog
  • Posts: 18
  • Joined: 13-September 12

Re: sort dictionary

Posted 14 September 2012 - 08:53 AM

sorted(d1.values()),sorted(d1.keys())
([111, 222, 333, 444, 555, 666, 777, 888, 999], ['a domain', 'b superkingdom', 'c kingdom', 'd phylum', 'e class', 'f order', 'g family', 'h genus', 'i species'])



Definitely. Here is another way to do it. Many others.
Was This Post Helpful? 0
  • +
  • -

#8 charles314  Icon User is offline

  • New D.I.C Head

Reputation: -11
  • View blog
  • Posts: 18
  • Joined: 13-September 12

Re: sort dictionary

Posted 14 September 2012 - 09:58 AM

You could print them like this:
for key, value in d1.iteritems():
	print key, value

Was This Post Helpful? 0
  • +
  • -

#9 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: sort dictionary

Posted 17 September 2012 - 06:17 AM

Here's a quick and simple example:
>>> d1 = {1:"1",5:"5",2:"2"}
>>> for key in sorted(d1.keys()):
	print(d1[key])

	
1
2
5


Was This Post Helpful? 0
  • +
  • -

#10 NunoSantos  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 04-September 12

Re: sort dictionary

Posted 17 September 2012 - 08:35 AM

thanks guys...i've created an array with the order that i want and then i used a for cicle like that

vectTaxonOrder = ['domain', 'superkingdom' ,'kingdom', 'phylum', 'class','subclass', 'order', 'suborder','family', 'subfamily','tribe','subtribe','genus', 'species','subspecies' ]
        for i in range (0, len(vectTaxonOrder)):
            if dtResult.has_key(vectTaxonOrder[i]):
                print "\nPresenting results for lever: "+str(vectTaxonOrder[i]).upper() + ": "
                for key in dtResult[vectTaxonOrder[i]]:
                    print "\t"+str(key)+": " + str(dtResult[vectTaxonOrder[i]][key])


thanks to all of you :)

This post has been edited by Simown: 17 September 2012 - 08:37 AM
Reason for edit:: Fixed code tags

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1