473,320 Members | 1,900 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

best Pythonic way to do this sort: Python newb

Hello all

I have build a list that contains data in the form below
-- simplified for question --
myList = [[value1, value2, value3],[value1, value2, value3], ...]

I have a function which takes value3 from the lists above and returns
another value. I want to use this returned value to sort the lists.

So, my resultant list would be ordered by the return value of the
function with value3 as its argument.

From a relative Python newb, what is the best way to do this?

Thanks for any help offered.
Oct 10 '05 #1
6 1665
"Sean Berry" <se**@buildingonline.com> writes:
myList = [[value1, value2, value3],[value1, value2, value3], ...]

I have a function which takes value3 from the lists above and returns
another value. I want to use this returned value to sort the lists.

So, my resultant list would be ordered by the return value of the
function with value3 as its argument.

From a relative Python newb, what is the best way to do this?


def get_key(x): return x[2]
sorted_list = sorted(myList, key=get_key)
Oct 10 '05 #2

"Paul Rubin" <http://ph****@NOSPAM.invalid> wrote in message
news:7x************@ruckus.brouhaha.com...
"Sean Berry" <se**@buildingonline.com> writes:
myList = [[value1, value2, value3],[value1, value2, value3], ...]

I have a function which takes value3 from the lists above and returns
another value. I want to use this returned value to sort the lists.

So, my resultant list would be ordered by the return value of the
function with value3 as its argument.

From a relative Python newb, what is the best way to do this?


def get_key(x): return x[2]
sorted_list = sorted(myList, key=get_key)


Sorry if I am missing something. But. what is sorted here?

My simplified function looks like this

def myFunction( myNumber ):
"do some math calculations to myNumber"
return "result of calculations"

So, I want to sort myList by the return of myFunction( value3 )

I tried doing the following... with no luck so far
myList.sort(lambda x, y: cmp(myFunction(x[2]), myFunction(y[2]))

Thanks for any help.
Oct 10 '05 #3
"Sean Berry" <se**@buildingonline.com> writes:
def get_key(x): return x[2]
sorted_list = sorted(myList, key=get_key)
Sorry if I am missing something. But. what is sorted here?


sorted is a built-in function that sorts the thing that you pass it.
It just appeared in Python 2.4, I think. With older versions, yeah,
you have to use the .sort method that sorts in place.
I tried doing the following... with no luck so far
myList.sort(lambda x, y: cmp(myFunction(x[2]), myFunction(y[2]))


That looks ok to me.
x = [(i,i*i,1 + 17*i**2 - i**3) for i in range(20)]
x [(0, 0, 1), (1, 1, 17), (2, 4, 61), (3, 9, 127), (4, 16, 209), (5, 25,
301), (6, 36, 397), (7, 49, 491), (8, 64, 577), (9, 81, 649), (10,
100, 701), (11, 121, 727), (12, 144, 721), (13, 169, 677), (14, 196,
589), (15, 225, 451), (16, 256, 257), (17, 289, 1), (18, 324, -323),
(19, 361, -721)] x.sort(lambda a,b:cmp(a[2],b[2]))
x [(19, 361, -721), (18, 324, -323), (0, 0, 1), (17, 289, 1), (1, 1,
17), (2, 4, 61), (3, 9, 127), (4, 16, 209), (16, 256, 257), (5, 25,
301), (6, 36, 397), (15, 225, 451), (7, 49, 491), (8, 64, 577), (14,
196, 589), (9, 81, 649), (13, 169, 677), (10, 100, 701), (12, 144,
721), (11, 121, 727)]

Oct 10 '05 #4
(sorted is a built-in function in 2.4)

def myFunction( data ):
""" Take one of your set of 3, grab [2] (the 3rd) and do calcs,
return value """
"do some math calculations to data[2]"
return "result of calculations"

sorted_list = sorted(myList, key=myFunction)

List is sorted in the order of the 'key' values, key being a value
returned from myFunction which operates on [2].

Oct 10 '05 #5

----
Satchidanand Haridas (sharidas at zeomega dot com)

ZeOmega (www.zeomega.com)
Open Minds' Open Solutions

Sean Berry wrote:
"Paul Rubin" <http://ph****@NOSPAM.invalid> wrote in message
news:7x************@ruckus.brouhaha.com...

"Sean Berry" <se**@buildingonline.com> writes:

myList = [[value1, value2, value3],[value1, value2, value3], ...]

I have a function which takes value3 from the lists above and returns
another value. I want to use this returned value to sort the lists.

So, my resultant list would be ordered by the return value of the
function with value3 as its argument.

From a relative Python newb, what is the best way to do this?

def get_key(x): return x[2]
sorted_list = sorted(myList, key=get_key)


Sorry if I am missing something. But. what is sorted here?

My simplified function looks like this

def myFunction( myNumber ):
"do some math calculations to myNumber"
return "result of calculations"

So, I want to sort myList by the return of myFunction( value3 )

I tried doing the following... with no luck so far
myList.sort(lambda x, y: cmp(myFunction(x[2]), myFunction(y[2]))

I think the above statement should be as follows:

myList.sort(lambda x, y: cmp(myFunction(x[2]) - myFunction(y[2]))

hope that helps.

regards,
Satchit
Oct 10 '05 #6
"Satchidanand Haridas" <sh******@zeomega.com> wrote:
So, I want to sort myList by the return of myFunction( value3 )

I tried doing the following... with no luck so far
myList.sort(lambda x, y: cmp(myFunction(x[2]), myFunction(y[2]))

I think the above statement should be as follows:

myList.sort(lambda x, y: cmp(myFunction(x[2]) - myFunction(y[2]))

hope that helps.


It would help more if you tested it before you posted. cmp takes two arguments (let alone that
subtraction may not be defined for the list elements), so the original version is correct.

George
Oct 10 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Daniel Pryde | last post by:
Hi everyone. I was wondering if anyone might be able to help me out here. I'm currently looking to find the quickest way to find a best fit match in a large array. My problem is that I have an...
11
by: Charles Krug | last post by:
I've a function that needs to maintain an ordered sequence between calls. In C or C++, I'd declare the pointer (or collection object) static at the function scope. What's the Pythonic way to...
8
by: Tim Chase | last post by:
When you have a set, known to be of length one, is there a "best" ("most pythonic") way to retrieve that one item? # given that I've got Python2.3. on hand, # hack the following two lines to get...
20
by: Joel Hedlund | last post by:
Hi all! I use python for writing terminal applications and I have been bothered by how hard it seems to be to determine the terminal size. What is the best way of doing this? At the end I've...
4
by: Carl J. Van Arsdall | last post by:
It seems the more I come to learn about Python as a langauge and the way its used I've come across several discussions where people discuss how to do things using an OO model and then how to design...
3
by: micklee74 | last post by:
hi I have a file with columns delimited by '~' like this: 1SOME STRING ~ABC~12311232432D~20060401~00000000 2SOME STRING ~DEF~13534534543C~20060401~00000000 3SOME STRING ...
5
by: akameswaran | last post by:
Disclaimer - I recognize this is not a practical exercise. There are many implementations around that would do the job better, more efficiently (Meaning in C) or whatever. I caught some thread...
9
by: Tim Daneliuk | last post by:
So it is claimed: http://www.infoq.com/news/Scala--combing-the-best-of-Ruby-;jsessionid=CC7C8366455E67B04EE5864B7319F5EC Has anyone taken a look at this that can provide a meaningful contrast...
32
by: David Isaac | last post by:
I have no experience with database applications. This database will likely hold only a few hundred items, including both textfiles and binary files. I would like a pure Python solution to the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.