Sorting and list comprehension

T

Tran Tuan Anh

Hi all,

I would like to do the followings, but I am quite new to Python hence
have not figured it out how...

salary = dictionary
salary["Bob"] = 11
salary["Marry"] = 4
salary["me"]= 45

How can I have a sort_value() function acts like this:
result = sortvalue(salary)
result = ["Marry","Bob","me"]

In sort, I would like to sort the key according to the values.

Regarding the list construction, how can I do this:

list = [item for item in list1 if for all item2 in list2 NOT
bad_condition(item, item2]

Anyone has any idea?
Thanks a lot!
Tuan-Anh
 
P

Peter Otten

Tran said:
Hi all,

I would like to do the followings, but I am quite new to Python hence
have not figured it out how...

salary = dictionary
salary["Bob"] = 11
salary["Marry"] = 4
salary["me"]= 45

How can I have a sort_value() function acts like this:
result = sortvalue(salary)
result = ["Marry","Bob","me"]

In sort, I would like to sort the key according to the values.
.... tmp = [(key(v), i, v) for (i, v) in enumerate(items)]
.... tmp.sort()
.... return [v[2] for v in tmp]
....['Mary', 'Bob', 'me']

Python 2.4 will have a similar sorted() function as a builtin.
Regarding the list construction, how can I do this:

list = [item for item in list1 if for all item2 in list2 NOT
bad_condition(item, item2]

I would use a conventional loop:
list1 = [1, 2, 3, 4]
list2 = [6, 2]
result = []
def bad_condition(a, b):
.... return a*2 == b
........ for item2 in list2:
.... if bad_condition(item1, item2):
.... break
.... else:
.... result.append(item1)
....[2, 4]

By contrast, the listcomp solution is messy and does no short-circuiting:
[item1 for item1 in list1 if True not in [bad_condition(item1, item2)
for item2 in list2]]
[2, 4]

Peter
 
B

Bryan

Tran said:
Hi all,

I would like to do the followings, but I am quite new to Python hence
have not figured it out how...

salary = dictionary
salary["Bob"] = 11
salary["Marry"] = 4
salary["me"]= 45

How can I have a sort_value() function acts like this:
result = sortvalue(salary)
result = ["Marry","Bob","me"]

In sort, I would like to sort the key according to the values.


i would do it this way which follows the DSU pattern.
>>> salary = {}
>>> salary['Bob'] = 11
>>> salary['Marry'] = 4
>>> salary['me'] = 45
>>> tmp = [(v, k) for k, v in salary.items()]
>>> tmp.sort()
>>> print [k for v, k in tmp] ['Marry', 'Bob', 'me']
>>>

bryan
 
A

Alex Martelli

Tran Tuan Anh said:
Hi all,

I would like to do the followings, but I am quite new to Python hence
have not figured it out how...

salary = dictionary
salary["Bob"] = 11
salary["Marry"] = 4
salary["me"]= 45

How can I have a sort_value() function acts like this:
result = sortvalue(salary)
result = ["Marry","Bob","me"]

You could upgrade to Python 2.4 and define

def sortvalue(d):
return sorted(d, key=d.get)

(or code it inline). If you're stuck with Python 2.3,

def sortvalue(d):
aux = [ (v,k) for k, v in d.iteritems() ]
aux.sort()
return [ k for v, k in aux ]

is probably the best you can do.

Regarding the list construction, how can I do this:

list = [item for item in list1 if for all item2 in list2 NOT
bad_condition(item, item2]

One possibility (I think it's not that good...):

from itertools import ifilter, imap

[x for x in list1
if 23 not in imap(lambda x: 23,
ifilter(lambda y: not badcondition(x, y), list2))
]

I think it's too fancy and you should define an auxiliary function:

def itemisok(item, list2, bad_condition):
for y in list2:
if bad_condition(item, y): return False
return True

so that you can code

[x for x in list1 if itemisok(x, list2, bad_condition)]

or thereabouts...


Alex
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top