473,414 Members | 1,577 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,414 software developers and data experts.

Sorting and list comprehension

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
Jul 18 '05 #1
3 3787
Tran Tuan Anh wrote:
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.
def sorted(items, key): .... tmp = [(key(v), i, v) for (i, v) in enumerate(items)]
.... tmp.sort()
.... return [v[2] for v in tmp]
.... salary = dict(Bob=11, Mary=4, me=45)
result = sorted(salary, key=salary.__getitem__)
result ['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 item1 in list1: .... for item2 in list2:
.... if bad_condition(item1, item2):
.... break
.... else:
.... result.append(item1)
.... result [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

Jul 18 '05 #2
Tran Tuan Anh wrote:
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
Jul 18 '05 #3
Tran Tuan Anh <an***@hotmail.com> wrote:
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
Jul 18 '05 #4

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

Similar topics

23
by: Fuzzyman | last post by:
Pythons internal 'pointers' system is certainly causing me a few headaches..... When I want to copy the contents of a variable I find it impossible to know whether I've copied the contents *or*...
1
by: shalendra chhabra | last post by:
Hi, I just had a tryst with python. I was wondering if python is good enough to do this kind of job -- for it has extensive support of string and pattern matching, ordering and list handling. ...
35
by: Moosebumps | last post by:
Does anyone here find the list comprehension syntax awkward? I like it because it is an expression rather than a series of statements, but it is a little harder to maintain it seems. e.g. you...
7
by: Chris P. | last post by:
Hi. I've made a program that logs onto a telnet server, enters a command, and then creates a list of useful information out of the information that is dumped to the screen as a result of the...
6
by: jena | last post by:
hello, when i create list of lambdas: l=] then l() returns 'C', i think, it should be 'A' my workaround is to define helper class with __call__ method: class X: def __init__(self,s): self.s=s...
18
by: a | last post by:
can someone tell me how to use them thanks
4
by: Gregory Guthrie | last post by:
Sorry for a simple question- but I don't understand how to parse this use of a list comprehension. The "or" clauses are odd to me. It also seems like it is being overly clever (?) in using a...
4
by: bullockbefriending bard | last post by:
Given: class Z(object): various defs, etc. class ZList(list): various defs, etc. i would like to be able to replace
10
by: Debajit Adhikary | last post by:
I have two lists: a = b = What I'd like to do is append all of the elements of b at the end of a, so that a looks like: a =
4
by: beginner | last post by:
Hi All, If I have a list comprehension: ab= c = "ABC" print c
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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...
0
Oralloy
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,...
0
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...
0
isladogs
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...
0
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...

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.