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. 6 1672
"Sean Berry" <se**@buildingo nline.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)
"Paul Rubin" <http://ph****@NOSPAM.i nvalid> wrote in message
news:7x******** ****@ruckus.bro uhaha.com... "Sean Berry" <se**@buildingo nline.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(lam bda x, y: cmp(myFunction( x[2]), myFunction(y[2]))
Thanks for any help.
"Sean Berry" <se**@buildingo nline.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(lam bda 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)]
(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].
----
Satchidanand Haridas (sharidas at zeomega dot com)
ZeOmega ( www.zeomega.com)
Open Minds' Open Solutions
Sean Berry wrote: "Paul Rubin" <http://ph****@NOSPAM.i nvalid> wrote in message news:7x******* *****@ruckus.br ouhaha.com...
"Sean Berry" <se**@buildingo nline.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(la mbda x, y: cmp(myFunction( x[2]), myFunction(y[2]))
I think the above statement should be as follows:
myList.sort(lam bda x, y: cmp(myFunction( x[2]) - myFunction(y[2]))
hope that helps.
regards,
Satchit
"Satchidana nd Haridas" <sh******@zeome ga.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(la mbda x, y: cmp(myFunction( x[2]), myFunction(y[2])) I think the above statement should be as follows:
myList.sort(lam bda 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 array of, say, 600*400, which contains a value
at each point, and I need to find the value in that array which is closest
to the input value. It's...
|
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 do this?
Is there a better solution than putting the sequence at module scope?
|
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 a "set" object
>>> import sets
>>> set = sets.Set
>>> s = set()
>>> len(s)
|
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 included a code snippet from Chuck Blake 'ls' app in
python. It seems to do the job just fine on my comp, but regrettably,
I'm not sassy enough to...
|
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 software in a more
"Pythonic" way.
My question is, should we as python developers be trying to write code
that follows more of a python standard...
| |
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 ~ACD~14353453554G~20060401~00000000
......
What is the pythonic way to sort this type of structured text file?
|
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 about sorting and started thinking about shell
sort.... and as part of my trying to pythonise my mind and break my
java mindset
So I decided to...
|
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
with Python?
--
----------------------------------------------------------------------------
|
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 extent reasonable.
Suggestions?
Thank you,
Alan Isaac
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |