| | |
Indexing two lists
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Sep 2009
Posts: 10
Reputation:
Solved Threads: 0
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...
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!
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)
sortJobSize = sorted(jobSize) a=0 while a<len(length): n=0 while n < len(length): if jobSize[n] == sortJobSize[a]: length[a] = length[n] n+=1 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!
This kind of problem has already been posted in this forum. I call this "sorting by colums".
Suppose we have 2 lists
how to sort the first list and update the second list according to this sort ? There is a trick for this
Suppose we have 2 lists
python Syntax (Toggle Plain Text)
>>> jobSize = [ 5, 8, 3, 4, 1, 9, 7 ] >>> length = [ 500, 80, 300, 4, 10, 900, 70]
python Syntax (Toggle Plain Text)
>>> L = zip(*sorted(zip(tuple(jobSize), tuple(length)))) >>> sortJobSize, length = (list(t) for t in L) >>> sortJobSize [1, 3, 4, 5, 7, 8, 9] >>> length [10, 300, 4, 500, 70, 80, 900]
Last edited by Gribouillis; Sep 19th, 2009 at 6:20 pm.
An interesting variation on this is to write a function to generate a "sorting index":
python Syntax (Toggle Plain Text)
def sorting_index(sequence): return zip(*sorted((y, x) for (x, y) in enumerate(sequence)))[1] if __name__ == "__main__": jobSize = [5, 8, 3, 4, 1, 9, 7] length = [ 500, 80, 300, 4, 10, 900, 70] index = sorting_index(jobSize) print("job sizes: {0}".format(jobSize)) print("lengths: {0}".format(length)) print("sorted job sizes: {0}".format([jobSize[i] for i in index])) print("updated lengths: {0}".format([length[i] for i in index])) print("index: {0}".format(index)) """ my output ---> job sizes: [5, 8, 3, 4, 1, 9, 7] lengths: [500, 80, 300, 4, 10, 900, 70] sorted job sizes: [1, 3, 4, 5, 7, 8, 9] updated lengths: [10, 300, 4, 500, 70, 80, 900] index: (4, 2, 3, 0, 6, 1, 5) """
•
•
•
•
Nice, looks like Python3 has bit your fancy.
Sooner or later we all have to follow your example.
python Syntax (Toggle Plain Text)
def sorting_index(sequence): return list(zip(*sorted((y, x) for (x, y) in enumerate(sequence))))[1]
Last edited by Gribouillis; Sep 20th, 2009 at 1:04 pm.
![]() |
Similar Threads
- comparing lists (Python)
- Turn Off Indexing to Speed Up XP (Windows tips 'n' tweaks)
- Source code implementing Latent Semantic Indexing (C++)
- Domain Extensions - Does it make a difference in Search Engine Indexing? (Search Engine Optimization)
- Indexing in OS X 2 (OS X)
- making sorted lists (was: Help Me!) (C)
- new member here :) (C)
- stack of linked lists (C++)
Other Threads in the Python Forum
- Previous Thread: Mechanics of a python backend
- Next Thread: Help a Novice with function please
| Thread Tools | Search this Thread |
alarm assignment avogadro beginner bluetooth character cmd code customdialog cx-freeze data decimals dictionary directory dynamic error examples exe file float format function generator gnu graphics gui halp homework http ideas import input ip itunes java leftmouse line linux list lists logging loop maintain maze millimeter module mouse mysqldb number numbers output parsing path port prime programming projects push py2exe pygame pyglet pyqt python queue random recursion schedule screensaverloopinactive script scrolledtext slicenotation sqlite ssh stdout string strings sudokusolver table terminal text thread threading time tlapse tuple tutorial ubuntu unicode urllib urllib2 variable variables ventrilo verify vigenere web webservice wikipedia wxpython xlib






