473,397 Members | 2,116 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,397 software developers and data experts.

how to sorted by summed itemgetter(x)

for example, let
from operator import itemgetter
items = [('a', [5, 2]), ('c', [1]), ('b', [6]), ('d', [7])]
sorted(items, key = itemgetter(1))
get this back:
[('c', [1]), ('a', [5, 2]), ('b', [6]), ('d', [7])]

but
sorted(items, key = sum(itemgetter(1)))
raise a errer:
'operator.itemgetter' object is not iterable

how to sorted by summed itemgetter(1)?
maybe sorted(items,key = lambda x:sum(x[1]))
can't itemgetter be used here?

Jun 25 '07 #1
2 2002
Aldarion <Er*************@gmail.comwrites:
how to sorted by summed itemgetter(1)?
maybe sorted(items,key = lambda x:sum(x[1]))
can't itemgetter be used here?
You really want function composition, e.g.

sorted(items, key=sum*itemgetter(1))

where * is a composition operator (doesn't exist in Python).

You could write:

def compose(f,g):
return lambda *a,**k: f(g(*a,**k))

and then use

sorted(items, key=compose(sum,itemgetter(1)))

or spell it out inline:

sorted(items, key=lambda x: sum(itemgetter(1)(x)))

I'd probably do something like:

snd = itemgetter(1) # I use this all the time
sorted(items, key=lambda x: sum(snd(x)))
Jun 25 '07 #2
Thanks for the reply,I got it.
On 6 25 , 9 19 , Paul Rubin <http://phr...@NOSPAM.invalidwrote:
Aldarion <ErendisAldar...@gmail.comwrites:
how to sorted by summed itemgetter(1)?
maybe sorted(items,key = lambda x:sum(x[1]))
can't itemgetter be used here?

You really want function composition, e.g.

sorted(items, key=sum*itemgetter(1))

where * is a composition operator (doesn't exist in Python).

You could write:

def compose(f,g):
return lambda *a,**k: f(g(*a,**k))

and then use

sorted(items, key=compose(sum,itemgetter(1)))

or spell it out inline:

sorted(items, key=lambda x: sum(itemgetter(1)(x)))

I'd probably do something like:

snd = itemgetter(1) # I use this all the time
sorted(items, key=lambda x: sum(snd(x)))

Jun 25 '07 #3

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

Similar topics

57
by: Egor Bolonev | last post by:
why functions created with lambda forms cannot contain statements? how to get unnamed function with statements?
9
by: custard_pie | last post by:
I need help sorting a list...I just can't figure out how to sort a list and then return a list with the index of the sorted items in the list for example if the list I want to sort is I need to...
24
by: Lasse Vågsæther Karlsen | last post by:
I need to merge several sources of values into one stream of values. All of the sources are sorted already and I need to retrieve the values from them all in sorted order. In other words: s1 = ...
1
by: J L | last post by:
I want to create a sorted list whose values are themselves sorted lists. I wrote the following simple test program but it does not behave as I would expect. What I wanted to do was have the...
1
by: thomas | last post by:
Hi NG I what to used the sorted function, and im getting this error Traceback (most recent call last): File "F:\home\thomas\src\guisample\test1.py", line 59, in ? main() File...
13
by: vd12005 | last post by:
Hello, i would like to sort(ed) and reverse(d) the result of many huge dictionaries (a single dictionary will contain ~ 150000 entries). Keys are words, values are count (integer). i'm...
18
by: Hunk | last post by:
Would like some advice on the fillowing I have a sorted list of items on which i require to search and retrieve the said item and also modify an item based on its identity. I think an Map stl...
1
by: hedgracer | last post by:
have a query that has a <>0 in a summed number field. The query runs extremely slow (30+ seconds) when this parameter is in this field. If I eliminate this parameter the query runs in milliseconds....
1
by: Harold Howe | last post by:
Howdy all, The msdn help says this about SorteList<k,v>: "If the list is populated all at once from sorted data, SortedList is faster than SortedDictionary." My question is this: how do I...
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?
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
marktang
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,...
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
tracyyun
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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...

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.