Indexing two lists

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2009
Posts: 10
Reputation: Benderbrau is an unknown quantity at this point 
Solved Threads: 0
Benderbrau Benderbrau is offline Offline
Newbie Poster

Indexing two lists

 
0
  #1
Sep 19th, 2009
Hello, I'm posting this code so that maybe it will help someone else in the future. I spent about a week trying to figure this out, and Google searches were not helping. Hopefully anyone else wondering about this will see this post on a Google result.

The program I finished writing was to simulate memory allocation, using schemes called first fit and best fit. I had two lists: jobSize (how big the job was) and length (how long the job stays active). For best fit, you must sort the job sizes, and I was having trouble keeping track with which item in the length list went to which jobSize item.

Anyway, if that made sense, here's what I did to sort the length list according to how the jobSize list was sorted...

  1. sortJobSize = sorted(jobSize)
  2. a=0
  3. while a<len(length):
  4. n=0
  5. while n < len(length):
  6. if jobSize[n] == sortJobSize[a]:
  7. length[a] = length[n]
  8. n+=1
  9. a+=1

As you can see, it's very simple and I'm amazed it took me so long to hammer out a working algorithm. Basically, it will take one item from jobSize at a time and compare it to each item in the sorted list. For every item that matches, it will change the location of length's item according to the index of sortJobSize. Hope this helps someone!
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 930
Reputation: Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough 
Solved Threads: 216
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Posting Shark

Re: Indexing two lists

 
1
  #2
Sep 19th, 2009
This kind of problem has already been posted in this forum. I call this "sorting by colums".
Suppose we have 2 lists
  1. >>> jobSize = [ 5, 8, 3, 4, 1, 9, 7 ]
  2. >>> length = [ 500, 80, 300, 4, 10, 900, 70]
how to sort the first list and update the second list according to this sort ? There is a trick for this
  1. >>> L = zip(*sorted(zip(tuple(jobSize), tuple(length))))
  2. >>> sortJobSize, length = (list(t) for t in L)
  3. >>> sortJobSize
  4. [1, 3, 4, 5, 7, 8, 9]
  5. >>> length
  6. [10, 300, 4, 500, 70, 80, 900]
Last edited by Gribouillis; Sep 19th, 2009 at 6:20 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 10
Reputation: Benderbrau is an unknown quantity at this point 
Solved Threads: 0
Benderbrau Benderbrau is offline Offline
Newbie Poster

Re: Indexing two lists

 
0
  #3
Sep 19th, 2009
Awesome, thanks! I tried something similar, but never got it working.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,026
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 offline Offline
DaniWeb's Hypocrite

Re: Indexing two lists

 
0
  #4
Sep 19th, 2009
Nice deduction indeed! Thanks Gribouillis!
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 112
Reputation: AutoPython is an unknown quantity at this point 
Solved Threads: 9
AutoPython's Avatar
AutoPython AutoPython is offline Offline
Junior Poster

Re: Indexing two lists

 
0
  #5
Sep 20th, 2009
Wow! So is that one of those things you just have learn by memory? I kind of like Benderbrau's though because it's more readable and you can actually see what's going on. But Gribouillis' way is very efficient and mind boggling.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 930
Reputation: Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough 
Solved Threads: 216
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Posting Shark

Re: Indexing two lists

 
0
  #6
Sep 20th, 2009
An interesting variation on this is to write a function to generate a "sorting index":
  1. def sorting_index(sequence):
  2. return zip(*sorted((y, x) for (x, y) in enumerate(sequence)))[1]
  3.  
  4. if __name__ == "__main__":
  5. jobSize = [5, 8, 3, 4, 1, 9, 7]
  6. length = [ 500, 80, 300, 4, 10, 900, 70]
  7.  
  8. index = sorting_index(jobSize)
  9. print("job sizes: {0}".format(jobSize))
  10. print("lengths: {0}".format(length))
  11. print("sorted job sizes: {0}".format([jobSize[i] for i in index]))
  12. print("updated lengths: {0}".format([length[i] for i in index]))
  13. print("index: {0}".format(index))
  14.  
  15. """ my output --->
  16. job sizes: [5, 8, 3, 4, 1, 9, 7]
  17. lengths: [500, 80, 300, 4, 10, 900, 70]
  18. sorted job sizes: [1, 3, 4, 5, 7, 8, 9]
  19. updated lengths: [10, 300, 4, 500, 70, 80, 900]
  20. index: (4, 2, 3, 0, 6, 1, 5)
  21. """
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,279
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 176
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Re: Indexing two lists

 
0
  #7
Sep 20th, 2009
Nice, looks like Python3 has bit your fancy.
Sooner or later we all have to follow your example.
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 930
Reputation: Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough 
Solved Threads: 216
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Posting Shark

Re: Indexing two lists

 
0
  #8
Sep 20th, 2009
Originally Posted by sneekula View Post
Nice, looks like Python3 has bit your fancy.
Sooner or later we all have to follow your example.
In fact, the above code doesn't run with python 3. You must modify the function like this
  1. def sorting_index(sequence):
  2. return list(zip(*sorted((y, x) for (x, y) in enumerate(sequence))))[1]
because zip doesn't return a list in 3.1
Last edited by Gribouillis; Sep 20th, 2009 at 1:04 pm.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC