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

Order of pytho objects list

  • 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
Jun 17 '10 #1
3 1477
Glenton
391 Expert 256MB
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):

Expand|Select|Wrap|Line Numbers
  1. import bisect
  2.  
  3. class whatever:
  4.     def __init__(self,num):
  5.         self.num=num
  6.     def __repr__(self):
  7.         return "whatever %d"%self.num
  8.  
  9. ob1=whatever(5)
  10. ob2=whatever(3)
  11. ob3=whatever(4)
  12.  
  13. list1=[ob1,ob2,ob3]
  14.  
  15. def mysort(myl):
  16.     ans=[]
  17.     order=[]
  18.     for ob in myl:
  19.         i=bisect.bisect_left(order,ob.num)
  20.         order.insert(i,ob.num)
  21.         ans.insert(i,ob)
  22.     return ans
  23.  
  24. print "original:",list1
  25. list2=mysort(list1)
  26. print "sorted:",list2
  27.  
This prints out
Expand|Select|Wrap|Line Numbers
  1. original: [whatever 5, whatever 3, whatever 4]
  2. sorted: [whatever 3, whatever 4, whatever 5]
Jun 18 '10 #2
It would have take a long time to figure it out myself.
Thanks a lot.
Jun 18 '10 #3
bvdet
2,851 Expert Mod 2GB
Here's an example of sorting on a key in Python 2.4 and higher:
Expand|Select|Wrap|Line Numbers
  1. >>> class Obj(object):
  2. ...     def __init__(self, num):
  3. ...         self.num = num
  4. ...         
  5. >>> objects = [Obj(6), Obj(2), Obj(12), Obj(4)]
  6. >>> objects
  7. [<__main__.Obj object at 0x034C7510>, <__main__.Obj object at 0x034C7410>, <__main__.Obj object at 0x034C7350>, <__main__.Obj object at 0x034C7310>]
  8. >>> [item.num for item in objects]
  9. [6, 2, 12, 4]
  10. >>> objects.sort(key=lambda x: x.num)
  11. >>> objects
  12. [<__main__.Obj object at 0x034C7410>, <__main__.Obj object at 0x034C7310>, <__main__.Obj object at 0x034C7510>, <__main__.Obj object at 0x034C7350>]
  13. >>> [item.num for item in objects]
  14. [2, 4, 6, 12]
  15. >>> 
Jun 18 '10 #4

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

Similar topics

9
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...
4
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};...
10
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...
2
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...
7
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...
1
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. ...
5
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...
19
beacon
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...
2
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...
1
tlhintoq
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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$) { } ...
0
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
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
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
jinu1996
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...

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.