943,902 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 535
  • Python RSS
Sep 19th, 2009
0

Indexing two lists

Expand Post »
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...

python Syntax (Toggle Plain Text)
  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!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Benderbrau is offline Offline
13 posts
since Sep 2009
Sep 19th, 2009
1

Re: Indexing two lists

This kind of problem has already been posted in this forum. I call this "sorting by colums".
Suppose we have 2 lists
python Syntax (Toggle Plain Text)
  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
python Syntax (Toggle Plain Text)
  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.
Reputation Points: 930
Solved Threads: 668
Posting Maven
Gribouillis is offline Offline
2,655 posts
since Jul 2008
Sep 19th, 2009
0

Re: Indexing two lists

Awesome, thanks! I tried something similar, but never got it working.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Benderbrau is offline Offline
13 posts
since Sep 2009
Sep 19th, 2009
0

Re: Indexing two lists

Nice deduction indeed! Thanks Gribouillis!
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Sep 20th, 2009
0

Re: Indexing two lists

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.
Reputation Points: 14
Solved Threads: 17
Junior Poster
AutoPython is offline Offline
138 posts
since Sep 2009
Sep 20th, 2009
0

Re: Indexing two lists

An interesting variation on this is to write a function to generate a "sorting index":
python Syntax (Toggle Plain Text)
  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. """
Reputation Points: 930
Solved Threads: 668
Posting Maven
Gribouillis is offline Offline
2,655 posts
since Jul 2008
Sep 20th, 2009
0

Re: Indexing two lists

Nice, looks like Python3 has bit your fancy.
Sooner or later we all have to follow your example.
Reputation Points: 961
Solved Threads: 211
Nearly a Posting Maven
sneekula is offline Offline
2,413 posts
since Oct 2006
Sep 20th, 2009
0

Re: Indexing two lists

Click to Expand / Collapse  Quote originally posted by sneekula ...
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
python Syntax (Toggle Plain Text)
  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.
Reputation Points: 930
Solved Threads: 668
Posting Maven
Gribouillis is offline Offline
2,655 posts
since Jul 2008

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: Mechanics of a python backend
Next Thread in Python Forum Timeline: Help a Novice with function please





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


Follow us on Twitter


© 2011 DaniWeb® LLC