Hi all,
I need to pass operator.itemgetter(item,...) multiple arguments but the number of arguments is variable. -
ivlist=['var1','var2','var3'] # This variable changes length
-
keyindices=range(-len(ivlist)-1,0) # = [-4, -3, -2, -1] in this example
-
-
# What I want in this example is
-
M_keys=M.keys()
-
for key in sorted(M_keys,key=operator.itemgetter(-4, -3, -2, -1)):
-
# 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.
3 5826
I figured it out. -
for key in sorted(M_keys,key=operator.itemgetter(*keyindices)):
-
I figured it out. -
for key in sorted(M_keys,key=operator.itemgetter(*keyindices)):
-
Glad you fixed your problem. :)
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. -
def SortKeyByIndices(keylist,indices):
-
sortedkeys=sorted(keylist,key=operator.itemgetter(indices[0]))
-
if len(indices)>1:
-
teeth=[]
-
teeth.append([sortedkeys[0][indices[0]],0])
-
for ikey, key in enumerate(sortedkeys):
-
if key[indices[0]]!=teeth[-1][0]:
-
teeth.append([key[indices[0]],ikey])
-
teeth.append([teeth[-1][0],len(sortedkeys)])
-
for tnum, tooth in enumerate(teeth[:-1]):
-
subkeys=sortedkeys[teeth[tnum][1]:teeth[tnum+1][1]]
-
subkeys=SortKeyByIndices(subkeys,indices[1:])
-
sortedkeys[teeth[tnum][1]:teeth[tnum+1][1]]=subkeys
-
return sortedkeys
-
Usage -
mykeys=mylist.keys()
-
mykeyssorted=SortKeyByIndices(mykeys,[1,2,5,6])
-
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: 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,...
|
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...
| |
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,...
|
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...
|
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,...
|
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: 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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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 ...
| |