Re: Sorting a list of lists
MRAB wrote: You'd have to post an example of that, but you could try deleting some of the entries before sorting so see whether you can still reproduce the problem with a smaller list. John Posner wrote: Please cut-and-paste the exact error message (or other evidence of failure) into a message. Thank you both. It appears that the problem was in the way the data were being read in from a file, than in the sorting itself. Once I fixed that, the results are as expected. Chandra -- http://mail.python.org/mailman/listinfo/python-list
Sorting a list of lists
Dear Folks, I have lines of values like so: 14, [25, 105, 104] 10, [107, 106, 162] 21, [26, 116, 165] I need to sort them in two ways: (a) By the numeric value of the first column; and (b) by the sum of the elements of the second item in each list, which is a list in itself. At present, I have appended each line into a list L so that I for teh above minimal data, I have a list of lists thus: [[14, [25, 105, 104]], [10, [107, 106, 162]], [21, [26, 116, 165]]] I have tried using (a) sorted(L, key = lambda x:(x[0])) and (b) sorted(L, key = lambda x:(sum(x[1]))) and get the anticipated results for (a) and (b0, at least with the above minimal data set. Is this a sensible way to go about the sorting, or are there better ways of doing it in Python? Also, I am baffled because the above fails obviously when len(L) is about a hundred and I can't figure out why. TIA Chandra -- http://mail.python.org/mailman/listinfo/python-list
Re: Sorting a list of lists
R (Chandra) Chandrasekhar wrote: Dear Folks, I have lines of values like so: 14, [25, 105, 104] 10, [107, 106, 162] 21, [26, 116, 165] I need to sort them in two ways: (a) By the numeric value of the first column; and (b) by the sum of the elements of the second item in each list, which is a list in itself. At present, I have appended each line into a list L so that I for teh above minimal data, I have a list of lists thus: [[14, [25, 105, 104]], [10, [107, 106, 162]], [21, [26, 116, 165]]] I have tried using (a) sorted(L, key = lambda x:(x[0])) and (b) sorted(L, key = lambda x:(sum(x[1]))) and get the anticipated results for (a) and (b0, at least with the above minimal data set. Is this a sensible way to go about the sorting, or are there better ways of doing it in Python? It's the obvious way to do it. Also, I am baffled because the above fails obviously when len(L) is about a hundred and I can't figure out why. You'd have to post an example of that, but you could try deleting some of the entries before sorting so see whether you can still reproduce the problem with a smaller list. -- http://mail.python.org/mailman/listinfo/python-list
Re: Sorting a list of lists
On 2/12/2010 12:45 PM, R (Chandra) Chandrasekhar wrote: Dear Folks, I have lines of values like so: 14, [25, 105, 104] 10, [107, 106, 162] 21, [26, 116, 165] I need to sort them in two ways: (a) By the numeric value of the first column; and (b) by the sum of the elements of the second item in each list, which is a list in itself. At present, I have appended each line into a list L so that I for teh above minimal data, I have a list of lists thus: [[14, [25, 105, 104]], [10, [107, 106, 162]], [21, [26, 116, 165]]] I have tried using (a) sorted(L, key = lambda x:(x[0])) Just plain sorted(L) will work, because Python knows how to compare your [N, [N,N,N]]-format values: [14, [25, 105, 104]] (14, [25, 105, 103]] False [14, [25, 105, 104]] (14, [25, 105, 105]] True and (b) sorted(L, key = lambda x:(sum(x[1]))) That looks good. and get the anticipated results for (a) and (b0, at least with the above minimal data set. Is this a sensible way to go about the sorting, or are there better ways of doing it in Python? You're doing fine. Also, I am baffled because the above fails obviously when len(L) is about a hundred and I can't figure out why. Please cut-and-paste the exact error message (or other evidence of failure) into a message. Tx, John -- http://mail.python.org/mailman/listinfo/python-list
Re: sorting a list of lists
Thanks guys, dirty hack was what I needed to get a job done quickly. Nick -- http://mail.python.org/mailman/listinfo/python-list
sorting a list of lists
Hi, i would like to sort a list of lists. The list is first sorted on the second item in the sub-lists (which I can do), then on the third item (which I can't). eg. records = [['dog',1,2], ['chair',2,1], ['cat',1,3], ['horse',3,4], ['table',3,2], ['window',3,5]] I want sorted to [['dog',1,2], ['cat',1,3], ['chair',2,1], ['table', 3,2], ['horse',3,4], ['window',3,5]] To sort on the second item in the sub-lists I do the following pass1 = itemgetter(1) sorted(records, key=pass1) How can I then sort on the third item in the sub-lists whilst keeping the order on the second item? Nick -- http://mail.python.org/mailman/listinfo/python-list
Re: sorting a list of lists
[EMAIL PROTECTED] wrote: i would like to sort a list of lists. The list is first sorted on the second item in the sub-lists (which I can do), then on the third item (which I can't). eg. records = [['dog',1,2], ['chair',2,1], ['cat',1,3], ['horse',3,4], ['table',3,2], ['window',3,5]] I want sorted to [['dog',1,2], ['cat',1,3], ['chair',2,1], ['table', 3,2], ['horse',3,4], ['window',3,5]] To sort on the second item in the sub-lists I do the following pass1 = itemgetter(1) sorted(records, key=pass1) How can I then sort on the third item in the sub-lists whilst keeping the order on the second item? list.sort() is stable, so items = sorted(records, key=itemgetter(2)) # minor order first items.sort(key=itemgetter(1)) # major order should give the desired result (items with equal item[1] are sorted by item[2]. Python 2.5 also allows to pass multiple indices to itemgetter() sorted(records, key=itemgetter(1, 2)) IIRC for Python 2.4 you'd have to write your own key routine: def key(item): return item[1], item[2] sorted(records, key=key) Peter -- http://mail.python.org/mailman/listinfo/python-list
Re: sorting a list of lists
i would like to sort a list of lists. The list is first sorted on the second item in the sub-lists (which I can do), then on the third item (which I can't). Write a comparator instead of dirty hacks mylistoflist.sort(mycomparator) def mycomparator(a, b): #do -- http://mail.python.org/mailman/listinfo/python-list
Re: sorting a list of lists
iapain wrote: i would like to sort a list of lists. The list is first sorted on the second item in the sub-lists (which I can do), then on the third item (which I can't). Write a comparator instead of dirty hacks mylistoflist.sort(mycomparator) def mycomparator(a, b): #do Taking advantage of stable sorting is totally not a hack. The OP just tried the two sorting steps in the wrong order. Stefan -- http://mail.python.org/mailman/listinfo/python-list
Re: sorting a list of lists
Taking advantage of stable sorting is totally not a hack. The OP just tried the two sorting steps in the wrong order. I didnt say not to use stable sorting, but write a generic function and hacky code. It is always better to adopt a generic approach. -- http://mail.python.org/mailman/listinfo/python-list
Re: sorting a list of lists
On Aug 27, 2:41 pm, iapain [EMAIL PROTECTED] wrote: ... It is always better to adopt a generic approach. The above statement is incorrect. -- http://mail.python.org/mailman/listinfo/python-list
Re: sorting a list of lists
records = [['dog',1,2], ['chair',2,1], ['cat',1,3], ['horse',3,4], ... ['table',3,2], ['window',3,5]] sorted(records, key = lambda x: (x[1], x[2])) [['dog', 1, 2], ['cat', 1, 3], ['chair', 2, 1], ['table', 3, 2], ['horse', 3, 4], ['window', 3, 5]] [EMAIL PROTECTED] wrote: Hi, i would like to sort a list of lists. The list is first sorted on the second item in the sub-lists (which I can do), then on the third item (which I can't). eg. records = [['dog',1,2], ['chair',2,1], ['cat',1,3], ['horse',3,4], ['table',3,2], ['window',3,5]] I want sorted to [['dog',1,2], ['cat',1,3], ['chair',2,1], ['table', 3,2], ['horse',3,4], ['window',3,5]] To sort on the second item in the sub-lists I do the following pass1 = itemgetter(1) sorted(records, key=pass1) How can I then sort on the third item in the sub-lists whilst keeping the order on the second item? Nick -- http://mail.python.org/mailman/listinfo/python-list
Re: sorting a list of lists
En Mon, 27 Aug 2007 15:41:15 -0300, iapain [EMAIL PROTECTED] escribi�: Taking advantage of stable sorting is totally not a hack. The OP just tried the two sorting steps in the wrong order. I didnt say not to use stable sorting, but write a generic function and hacky code. It is always better to adopt a generic approach. Sorting on multiple keys from last to first has been the standard and generic approach even before computers existed. Hollerith punched cards processors did it that way around 1890. -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list
Re: Sorting a List of Lists
[EMAIL PROTECTED] a écrit : I can't seem to get this nailed down and I thought I'd toss it out there as, by gosh, its got to be something simple I'm missing. I have two different database tables of events that use different schemas. I am using python to collate these records for display. I do this by creating a list of lists that look roughly like this: events = [['Event URL as String', 'Event Title as String ', Event Date as Datetime], ...] Then you should not use a list of lists, but a list of tuples. I then thought I'd just go events.sort(lambda x,y: x[2]y[2]) and call it a day. That didn't work. But then lamda functions like to be very simple, maybe object subscripts aren't allowed (even though I didn't get an error). So I wrote a comparison function that looks much as you would expect: def date_compare(list1, list2): x = list1[2] y = list2[2] if xy: return 1 elif x==y: return 0 else: # xy return -1 But as before sorting with this function returns None. What have I overlooked? Lol. I guess this is a FAQ. list.sort() performs a destructive in-place sort, and always return None. This is in the FineManual: [EMAIL PROTECTED]:~$ python Python 2.4.4c1 (#2, Oct 11 2006, 21:51:02) [GCC 4.1.2 20060928 (prerelease) (Ubuntu 4.1.1-13ubuntu5)] on linux2 Type help, copyright, credits or license for more information. help(list.sort) Help on method_descriptor: sort(...) L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*; cmp(x, y) - -1, 0, 1 You may want to use sorted(iterable, cmp=None, key=None, reverse=False) if you don't want to sort in-place. Also, using comparison functions is usually not the most efficient way to do such a sort. In your case, I'd go for a good old Decorate/sort/undecorate (AKA schwarzian transform): events = [evt for date, evt in sorted([(evt[2], evt) for evt in events])] HTH -- http://mail.python.org/mailman/listinfo/python-list
Re: Sorting a List of Lists
On Jan 31, 12:35 pm, Bruno Desthuilliers bruno. [EMAIL PROTECTED] wrote: [EMAIL PROTECTED] a écrit : I can't seem to get this nailed down and I thought I'd toss it out there as, by gosh, its got to be something simple I'm missing. I have two different database tables of events that use different schemas. I am using python to collate these records for display. I do this by creating a list of lists that look roughly like this: events = [['Event URL as String', 'Event Title as String ', Event Date as Datetime], ...] Then you should not use a list of lists, but a list of tuples. I then thought I'd just go events.sort(lambda x,y: x[2]y[2]) and call it a day. That didn't work. But then lamda functions like to be very simple, maybe object subscripts aren't allowed (even though I didn't get an error). So I wrote a comparison function that looks much as you would expect: def date_compare(list1, list2): x = list1[2] y = list2[2] if xy: return 1 elif x==y: return 0 else: # xy return -1 But as before sorting with this function returns None. What have I overlooked? Lol. I guess this is a FAQ. list.sort() performs a destructive in-place sort, and always return None. This is in the FineManual: [EMAIL PROTECTED]:~$ python Python 2.4.4c1 (#2, Oct 11 2006, 21:51:02) [GCC 4.1.2 20060928 (prerelease) (Ubuntu 4.1.1-13ubuntu5)] on linux2 Type help, copyright, credits or license for more information. help(list.sort) Help on method_descriptor: sort(...) L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*; cmp(x, y) - -1, 0, 1 You may want to use sorted(iterable, cmp=None, key=None, reverse=False) if you don't want to sort in-place. Also, using comparison functions is usually not the most efficient way to do such a sort. In your case, I'd go for a good old Decorate/sort/undecorate (AKA schwarzian transform): events = [evt for date, evt in sorted([(evt[2], evt) for evt in events])] HTH I agree with you B., but see the comments here: http://www.biais.org/blog/index.php/2007/01/28/23-python-sorting- efficiency for information on the relative speeds of rolling your own DSU versus using itemgetter and key=... - Paddy. -- http://mail.python.org/mailman/listinfo/python-list
Re: Sorting a List of Lists
Paddy a écrit : On Jan 31, 12:35 pm, Bruno Desthuilliers bruno. (snip) Also, using comparison functions is usually not the most efficient way to do such a sort. In your case, I'd go for a good old Decorate/sort/undecorate (AKA schwarzian transform): events = [evt for date, evt in sorted([(evt[2], evt) for evt in events])] HTH I agree with you B., but see the comments here: http://www.biais.org/blog/index.php/2007/01/28/23-python-sorting- efficiency for information on the relative speeds of rolling your own DSU versus using itemgetter and key=... Yeps, looks like 2.5 got a real speedup wrt/ itemgetter. Nice to know, and thanks for the link (BTW, this profileit() decorator looks pretty nice too !-) -- http://mail.python.org/mailman/listinfo/python-list
Sorting a List of Lists
I can't seem to get this nailed down and I thought I'd toss it out there as, by gosh, its got to be something simple I'm missing. I have two different database tables of events that use different schemas. I am using python to collate these records for display. I do this by creating a list of lists that look roughly like this: events = [['Event URL as String', 'Event Title as String ', Event Date as Datetime], ...] I then thought I'd just go events.sort(lambda x,y: x[2]y[2]) and call it a day. That didn't work. But then lamda functions like to be very simple, maybe object subscripts aren't allowed (even though I didn't get an error). So I wrote a comparison function that looks much as you would expect: def date_compare(list1, list2): x = list1[2] y = list2[2] if xy: return 1 elif x==y: return 0 else: # xy return -1 But as before sorting with this function returns None. What have I overlooked? -- http://mail.python.org/mailman/listinfo/python-list
Re: Sorting a List of Lists
On Jan 30, 5:55 pm, [EMAIL PROTECTED] wrote: I can't seem to get this nailed down and I thought I'd toss it out there as, by gosh, its got to be something simple I'm missing. I have two different database tables of events that use different schemas. I am using python to collate these records for display. I do this by creating a list of lists that look roughly like this: events = [['Event URL as String', 'Event Title as String ', Event Date as Datetime], ...] I then thought I'd just go events.sort(lambda x,y: x[2]y[2]) and call it a day. That didn't work. But then lamda functions like to be very simple, maybe object subscripts aren't allowed (even though I didn't get an error). So I wrote a comparison function that looks much as you would expect: def date_compare(list1, list2): x = list1[2] y = list2[2] if xy: return 1 elif x==y: return 0 else: # xy return -1 But as before sorting with this function returns None. What have I overlooked? All sorts return None. the sort is in place. Check your list post- sort. THN -- http://mail.python.org/mailman/listinfo/python-list
Re: Sorting a List of Lists
events = [['Event URL as String', 'Event Title as String ', Event Date as Datetime], ...] I then thought I'd just go events.sort(lambda x,y: x[2]y[2]) and call it a day. That didn't work. But then lamda functions like to be very simple, maybe object subscripts aren't allowed (even though I didn't get an error). So I wrote a comparison function that looks much as you would expect: Comparision functions must return -1, 0 or 1, not a bool. You may use a key function instead in this case (requires python 2.4 or newer): events.sort(key=lambda x: x[2]) Viktor -- http://mail.python.org/mailman/listinfo/python-list
Re: Sorting a List of Lists
[EMAIL PROTECTED] wrote: I can't seem to get this nailed down and I thought I'd toss it out there as, by gosh, its got to be something simple I'm missing. I have two different database tables of events that use different schemas. I am using python to collate these records for display. I do this by creating a list of lists that look roughly like this: events = [['Event URL as String', 'Event Title as String ', Event Date as Datetime], ...] I then thought I'd just go events.sort(lambda x,y: x[2]y[2]) and call it a day. That didn't work. But then lamda functions like to be very simple, maybe object subscripts aren't allowed (even though I didn't get an error). So I wrote a comparison function that looks much as you would expect: def date_compare(list1, list2): x = list1[2] y = list2[2] if xy: return 1 elif x==y: return 0 else: # xy return -1 But as before sorting with this function returns None. What have I overlooked? Sort doesn't return a list, it sorts in place. None is the result code (if you will) of the sort completion. -Larry -- http://mail.python.org/mailman/listinfo/python-list
Re: Sorting a List of Lists
Létezo [EMAIL PROTECTED] writes: I then thought I'd just go events.sort(lambda x,y: x[2]y[2]) and call Use: events.sort(lambda x,y: cmp(x[2], y[2])) or: events.sort(key=lambda x: x[2]) -- http://mail.python.org/mailman/listinfo/python-list