- I have a list of objects.
- Each object has a variable named number.
- All objects are in a list
What I would like to achieve in the most efficient manner is to order my objects by their number var.
An example :
Object1
_number=1
Object4
_number=2
Object3
_number=5
ListObj=[Object1,Object3,Object4]
What I want to get is to be able to get my objects in order by their number :
Object1, Object4, Object3
3 1477
It might be possible to do it by assigning the special methods that allow <, > and = to be used, and then sorting it. I haven't tried this.
But failing this, what you want to do is use the bisect module (wonderfully efficient): - import bisect
-
-
class whatever:
-
def __init__(self,num):
-
self.num=num
-
def __repr__(self):
-
return "whatever %d"%self.num
-
-
ob1=whatever(5)
-
ob2=whatever(3)
-
ob3=whatever(4)
-
-
list1=[ob1,ob2,ob3]
-
-
def mysort(myl):
-
ans=[]
-
order=[]
-
for ob in myl:
-
i=bisect.bisect_left(order,ob.num)
-
order.insert(i,ob.num)
-
ans.insert(i,ob)
-
return ans
-
-
print "original:",list1
-
list2=mysort(list1)
-
print "sorted:",list2
-
This prints out - original: [whatever 5, whatever 3, whatever 4]
-
sorted: [whatever 3, whatever 4, whatever 5]
It would have take a long time to figure it out myself.
Thanks a lot.
bvdet 2,851
Expert Mod 2GB
Here's an example of sorting on a key in Python 2.4 and higher: - >>> class Obj(object):
-
... def __init__(self, num):
-
... self.num = num
-
...
-
>>> objects = [Obj(6), Obj(2), Obj(12), Obj(4)]
-
>>> objects
-
[<__main__.Obj object at 0x034C7510>, <__main__.Obj object at 0x034C7410>, <__main__.Obj object at 0x034C7350>, <__main__.Obj object at 0x034C7310>]
-
>>> [item.num for item in objects]
-
[6, 2, 12, 4]
-
>>> objects.sort(key=lambda x: x.num)
-
>>> objects
-
[<__main__.Obj object at 0x034C7410>, <__main__.Obj object at 0x034C7310>, <__main__.Obj object at 0x034C7510>, <__main__.Obj object at 0x034C7350>]
-
>>> [item.num for item in objects]
-
[2, 4, 6, 12]
-
>>>
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Yomanium Yoth Taripoät II |
last post by:
HI,
1) what are the differences between list and tuple?
2) how to concatenate tuple and list? no method, no opérator?
3) im looking the fucking manual, and cant add value in my tuple, when it...
|
by: Brett |
last post by:
I have two arrays and i wish to sort the first one numerically, but after
sorting, I would like the second array to be in the same matching order as
the first array.
ie.
@l1={3,1,2};...
|
by: Diego F. |
last post by:
Hello. I need to store custom objects in a SQL Server 2000 table. Which is
the easiest way to do it? Do I need to write methods to store each attribute
separately from C# app to the table and the...
|
by: Robert W. |
last post by:
I've come across a problem that I just cannot explain. I'm hoping that
someone has seen it before.
I have the following data structure:
Questions (collection)
Question (object)
Choices...
|
by: Raider |
last post by:
Are global objects always constructed in order of they declaration?
I use one global in the constructor of another one and I want to be sure
it works the same with any compiler. The code looks...
|
by: Nemisis |
last post by:
hi guys,
Currently converting an old classic asp system to a OOP asp.net
application. We are building the new application using a 3 tier
arcitecture and i was wondering about the following.
...
|
by: Macca |
last post by:
Hi,
I have a number of objects i would like to store in a list like struture.
They already have ID's associated with them so i would like to utilise these
ids as accessors the list..
At the...
|
by: beacon |
last post by:
As you can probably tell from the title, I'm a little frustrated with linked lists. I'm working on a homework assignment and it just isn't making sense to me. I've read and read and read on the...
|
by: keshri |
last post by:
Hi Friends,
This is keshri here. I am pursuing MCA in final year and together I am getting training of Asp.Net and Windows Applications Forms with C#.
My question is : I want to create a windows...
|
by: tlhintoq |
last post by:
I'm pretty sure this is language independent and is going to be the same whether it's VC or C# - but my project is C# WIndows Forms just in case.
Does anyone have a good handle on the sequence of...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |