I read in the following thread that the "cmp" method is not needed for sorting and that "key" is egnough : http://bytes.com/topic/python/answer...rison-function
but if you need to compare program version, how do you do this with only "key" method ? -
versions = ["3.5", "1.0", "3.1.3", "1.2.0",
-
"15.15421.21.42.63.1.0.3.54"]
-
ordered = sorted(versions, key=???)
-
Thanks.
One way is to use functools.cmp_to_key: - from functools import cmp_to_key
-
-
def c(a,b):
-
a1 = [int(item) for item in a.split(".")]
-
b1 = [int(item) for item in b.split(".")]
-
for i in range(min(len(a1), len(b1))):
-
j = cmp(a1[i], b1[i])
-
if j:
-
return j
-
return cmp(a,b)
-
-
versions = ["3.5", "1.0", "3.1.3", "1.2.0", "15.15421.21.42.63.1.0.3.54"]
-
-
versions2 = sorted(versions, key=cmp_to_key(c))
-
-
print versions2
2 3618 bvdet 2,851
Expert Mod 2GB
One way is to use functools.cmp_to_key: - from functools import cmp_to_key
-
-
def c(a,b):
-
a1 = [int(item) for item in a.split(".")]
-
b1 = [int(item) for item in b.split(".")]
-
for i in range(min(len(a1), len(b1))):
-
j = cmp(a1[i], b1[i])
-
if j:
-
return j
-
return cmp(a,b)
-
-
versions = ["3.5", "1.0", "3.1.3", "1.2.0", "15.15421.21.42.63.1.0.3.54"]
-
-
versions2 = sorted(versions, key=cmp_to_key(c))
-
-
print versions2
Excellent!
That is just what I need.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Xah Lee |
last post by:
is it possible in Python to create a function that maintains a variable
value?
something like this:
globe=0;
def myFun():
globe=globe+1
return globe
|
by: David Rasmussen |
last post by:
I want to use sort() and supply my own comparison function, like
bool lessThan(const S& a, const S& b)
{
return value(a) < value(b);
}
and then sort by:
sort(a.begin(), a.end(), lessThan);
|
by: aurgathor |
last post by:
Howdy,
How do I pass some function a generic comparison
function? I figured out one non-generic case, but
since this code got parameter declarations in two places,
it's obviously not generic....
|
by: Spoon |
last post by:
Hello,
I've come across the following comparison function used as the 4th
parameter in qsort()
int cmp(const void *px, const void *py)
{
const double *x = px, *y = py;
return (*x > *y) -...
|
by: eastern_strider |
last post by:
I'm running into problems about defining a comparison function for a
map which has a user defined key. For example:
class Key {
public:
string name;
int number;
Key (na, nu) : name (na),...
|
by: SvenMathijssen |
last post by:
Hi,
I've been wrestling with a problem for some time that ought to be fairly simple, but turns out to be very difficult for me to solve. Maybe someone here knows the answer.
What I try to do is...
|
by: fade |
last post by:
Good afternoon,
I need some advice on the following:
I've got a class that has a member
std::vector<CStringm_vFileName and a member CString m_path;
The vector contains a bunch of filenames with...
|
by: PicO |
last post by:
how can i make a set with comparison function ?
all i know that i can make a map with comparison function like this
struct strCmp {
bool operator()( const char* s1, const char* s2 ) const {...
|
by: mdh |
last post by:
May I ask the following.
By K&R's own admission, the example used to describe function pointers
is complex ( on P119). In addition, the use of casts has been stated
by some on this group as...
|
by: Thomas Heller |
last post by:
Does Python 3 have no way anymore to sort with a comparison function?
Both .sort() and sorted() seem to accept only 'key' and 'reverse' arguments,
the 'cmp' argument seems to be gone. Can that...
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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
| |