473,503 Members | 1,818 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

operator.itemgetter() syntax

15 New Member
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
  3.  
  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
  8.  
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 5826
rogerlew
15 New Member
I figured it out.
Expand|Select|Wrap|Line Numbers
  1. for key in sorted(M_keys,key=operator.itemgetter(*keyindices)):
  2.  
Jun 6 '07 #2
ilikepython
844 Recognized Expert Contributor
I figured it out.
Expand|Select|Wrap|Line Numbers
  1. for key in sorted(M_keys,key=operator.itemgetter(*keyindices)):
  2.  
Glad you fixed your problem. :)
Jun 6 '07 #3
rogerlew
15 New Member
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
  15.  
Usage
Expand|Select|Wrap|Line Numbers
  1. mykeys=mylist.keys()
  2. mykeyssorted=SortKeyByIndices(mykeys,[1,2,5,6])
  3.  
Jun 8 '07 #4

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

Similar topics

19
10140
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. ...
17
3464
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 ,...
5
1445
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...
18
1482
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; ...
1
1790
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; ...
14
2781
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{
2
20130
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...
12
438
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...
17
1602
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...
0
7202
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
7086
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
7280
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,...
1
6991
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
5578
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,...
1
5014
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
3167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1512
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.