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
x>y:
return
1
elif
x==y:
return
0
else: #
x<y
return -1
But as before sorting with this function returns None.
What have I overlooked? 7 2171
On Jan 30, 5:55 pm, apoth...@gmail.com 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
x>y:
return
1
elif
x==y:
return
0
else: #
x<y
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
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 ap******@gmail.com 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
x>y:
return
1
elif
x==y:
return
0
else: #
x<y
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
Létezo <le****@fw.huwrites:
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]) ap******@gmail.com 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
x>y:
return
1
elif
x==y:
return
0
else: #
x<y
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:
bruno@bruno:~$ 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
On Jan 31, 12:35 pm, Bruno Desthuilliers <bruno.
42.desthuilli...@wtf.websiteburo.oops.comwrote:
apoth...@gmail.com 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
x>y:
return
1
elif
x==y:
return
0
else: #
x<y
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:
bruno@bruno:~$ 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/...ython-sorting-
efficiency
for information on the relative speeds of rolling your own DSU versus
using itemgetter and key=...
- Paddy.
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/...ython-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 !-) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Philippe C. Martin |
last post by:
Hi,
I'm looking for an easy algorithm - maybe Python can help:
I start with X lists which intial sort is based on list #1.
I want to reverse sort list #1 and have all other lists sorted...
|
by: Kakarot |
last post by:
I'm gona be very honest here, I suck at programming, *especially* at
C++. It's funny because I actually like the idea of programming ...
normally what I like I'm atleast decent at. But C++ is a...
|
by: Foodbank |
last post by:
Hi everyone. I'm having trouble with this radix sorting program. I've
gotten some of it coded except for the actual sorting :( The book I'm
teaching myself with (Data Structures Using C and...
|
by: xrow |
last post by:
Hello
I have a simple webservice / c# application that receives data from server and prints the data in the asp:datagrid control
I have problem when sorting data in datagrid
I have created...
|
by: FBM |
last post by:
Hi,
I am working on a program that simulates one of the elements of ATM.
The simulation stores events which occurs every some milliseconds for a
certain amount of time. Every time that an event...
|
by: Susan.Adkins |
last post by:
Alright, I must say that the problem my teacher has given us to do has
royally annoyed me.
We are to take a letter *txt file* and then read in the words.
Make 2 seperate lists *even and odd* for...
|
by: George Sakkis |
last post by:
It would be useful if list.sort() accepted two more optional
parameters, start and stop, so that you can sort a slice in place. In
other words,
x = range(1000000)
x.sort(start=3, stop=-1)
...
|
by: acosgaya |
last post by:
Hi, I would like some help as how to approach the following problem:
I have a set of d-dimensional points (e.g (3,5,8,9,5) is a 5-dimensional point), and I need to sort the points on each of the...
|
by: martin-g |
last post by:
Hi.
Mostly I program in C++, and I'm not fluent in C# and .NET. In my last
project I began to use LinkedList<and suddenly noticed that can't
find a way to sort it. Does it mean I must implement...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
| |