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

operator.itemgetter() syntax

Hi all,

I need to pass operator.itemgetter(item,...) multiple arguments but the number of arguments is variable.

Expand|Select|Wrap|Line Numbers
  1. ivlist=['var1','var2','var3'] # This variable changes length
  2. keyindices=range(-len(ivlist)-1,0) # = [-4, -3, -2, -1] in this example
  4. # What I want in this example is
  5. M_keys=M.keys()
  6. for key in sorted(M_keys,key=operator.itemgetter(-4, -3, -2, -1)):
  7.     # for loop code
What is the syntax for passing operator.itemgetter(items,...) the values of the keyindices list as separate arguments?

Thank you for your help.
Jun 6 '07 #1
3 5802
I figured it out.
Expand|Select|Wrap|Line Numbers
  1. for key in sorted(M_keys,key=operator.itemgetter(*keyindices)):
Jun 6 '07 #2
844 Expert 512MB
I figured it out.
Expand|Select|Wrap|Line Numbers
  1. for key in sorted(M_keys,key=operator.itemgetter(*keyindices)):
Glad you fixed your problem. :)
Jun 6 '07 #3
The method described above works for Python 2.5 but not for 2.4.4. Operator.itemgetter only accepts one argument under 2.4.4. Another problem I found was that with 2.5 the * will only flatten a sequence. So if you want to sort by unordered a set of unordered indices (e.g. -1, 4,5,6) your out of luck (from want I tried at least).

I ended writing a function which works with 2.4 and 2.5. I am posting it here encase someone runs into the same problem and happens to google this in the future.

Expand|Select|Wrap|Line Numbers
  1. def SortKeyByIndices(keylist,indices):
  2.     sortedkeys=sorted(keylist,key=operator.itemgetter(indices[0]))
  3.     if len(indices)>1:
  4.         teeth=[]
  5.         teeth.append([sortedkeys[0][indices[0]],0])
  6.         for ikey, key in enumerate(sortedkeys):
  7.             if key[indices[0]]!=teeth[-1][0]:
  8.                 teeth.append([key[indices[0]],ikey])
  9.         teeth.append([teeth[-1][0],len(sortedkeys)])
  10.         for tnum, tooth in enumerate(teeth[:-1]):
  11.             subkeys=sortedkeys[teeth[tnum][1]:teeth[tnum+1][1]]
  12.             subkeys=SortKeyByIndices(subkeys,indices[1:])
  13.             sortedkeys[teeth[tnum][1]:teeth[tnum+1][1]]=subkeys
  14.     return sortedkeys
Expand|Select|Wrap|Line Numbers
  1. mykeys=mylist.keys()
  2. mykeyssorted=SortKeyByIndices(mykeys,[1,2,5,6])
Jun 8 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

by: jacob navia | last post by:
C++ introduced an interesting feature (among others): operator overloading. The idea is to build a mechanism for the user defining its own number types and the operations to be done with them. ...
by: Anoob | last post by:
Can we consider () unary operator when calling a function, in exps eq. f(), ( 1 + 2). But when we call function f( 10 ) it is a binary operator. Even if we pass f( 10, 20) as we are using ,...
by: Rob | last post by:
To follow up on the "copy constructor clarification thread"... The assignment operator syntax shown previously: MyClass% operator=(const MyClass%); seems to have problems if you have member...
by: Marchello | last post by:
I write 2-dimensional array's wrapper class. And how I can access to the one element through the operator ? I need something like this: CMyArray<int> A(4, 4); // array "int A"; int value = A; ...
by: sd2004 | last post by:
Could someone please help ? I am getting compiling error but do not understand why ? below is snipset of code: istream& operator>>(istream& is, test_ins& s){ is >>s.pass>>s.put>>s.type; ...
by: Jess | last post by:
Hi, I read about operator overloading and have a question regarding "operator->()". If I have two classes like this: struct A{ void f(); }; struct B{
by: aaragon | last post by:
Hi everyone, Can someone point me out why I can't declare the operator() of a functor as static? The reason behind this is that I want to be able to call to the function without instantiating...
by: subramanian100in | last post by:
For a class Test, we write the assignment operator as Test & Test::operator=(const Test & rhs); instead of const Test & Test::operator=(const Test & rhs); that is, the return type of...
by: Corey Cooper | last post by:
I have a class for which I needed to create an operator= function. I then use that as a base class for another class, which also needs an operator=. What is the syntax to use in the derived class...
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.