Is Python Smart Enough to do Sorting like this?

Gary Herron gherron at islandtraining.com
Sat Mar 13 12:18:59 EST 2004


On Friday 12 March 2004 05:26 pm, shalendra chhabra wrote:
> Hi,
> I just had  a tryst with python.
> I was wondering if python is good enough to do this kind of job -- for it
> has extensive support of string and pattern matching, ordering and list
> handling.
>
> There are 'n' itemsets, the size of n is unknown. The program can count
> itself while scanning.  Each of the n itemset is of the form key: value
>
> {key1: value1, key2:value2, key3, value3, key4: value4 .....}
>
> Is it possible to order this itemset in an increasing order of key: value
> with respect to keys. For example: if
> key4>key2>key3>key1
> then the resulting ordering should be in such a way:
>
> key4:value4, key2: value2,key3:value3>key1: value1
>
> I plan to do this by storing this whole set of itemset in a vector or map.
>
> Lets say vector v={key1:value1, key2: value2, key3: value3,key4:value4}
>
> Now I wish to apply the sorting over this vector v, such that I have the
> entries in the increasing order with respect to keys in v again.
> Can some one point to me how to achieve this with python?
> A snippet.....
>

Lots of possible answers here since it's hard to tell what you really
want. The upshot is that either built in *lists* or *dictionaries*
(and many more structures that you could invent), will allow you tools
to access the items in sorted order.  Which data structure you
actually choose is governed by the other interactions you desire.

You could store the key,value pairs in a Python dictionary like this:

  d = {key1:value1, key2: value2, key3: value3,key4:value4}

of in a Python list of tuples like this:

  v = [ (key1,value1), (key2, value2), (key3,value3), (key4,value4) ]


But now for the hard questions:

  Do you really care how they are actually stored in memory?
	 Probably not -- see the next question.

  Do you care how your program will access them efficiently in sorted order?
	 Either data structure can be accessed in sorted order.  For the dictionary
			keys = d.keys()              # Get the keys
			keys.sort()                  # Sort the keys
			for key in keys:             # Access via sorted keys
			  --- do something with pair key,d[key] ---
     and for the list:
			v.sort()                     # Sort the key,value tuples
			for key,value in v:			 # Access the pairs in sorted order
			  --- do something with pair key,value ---

  Do you care how you build them or otherwise interact with them?
    Both dictionaries and lists are data structures rich with methods
    for interaction.  They supply various methods for building,
    accessing, inserting, deleting, slicing, and much more.  Here's an
    outline of a small portion of their methods
     Dictionary support access by key:
				insert:  d[newkey] = newvalue
				delete:  del d[key]
				access:  d[key]
	 Lists support access by index:
				insert:  d[i:i] = newvalue
				append:  d.append(newvalue)
				delete:  del d[i]
				access:  d[i]
				slices:  d[i:j]

So the questions you really need to ask and answer are:

  How will you build your structure?
  How will you otherwise interact with the structure?


Hope that helps,
Gary Herron







More information about the Python-list mailing list