list - Overriding sorting attribute in class, python -


i use bisect (as shown here, in second answer: does python have sorted list?), instead of using list of numbers, have list of objects. in specific, objects class: https://networkx.github.io/documentation/latest/_modules/networkx/classes/graph.html

i list keep graphs sorted number of nodes. if push these graphs list, looks being inserted in arbitrary way, (if run many times, changes between runs).

is there "sort" function each class can define, when applying sorting used (like operator overriding in other languages) ?

import bisect import networkx nx  l=[] g1 = nx.graph() g2 = nx.graph()  g1.add_edges_from([(1,2),(1,3),(2,3),(3,4),(4,5),(4,6),(5,6),(4,7),(7,8),(7,9),(8,9)]) print 'g1', g1.number_of_nodes() g2.add_edges_from([(1,2),(1,3)]) print 'g2', g2.number_of_nodes()  bisect.insort(l,g1) bisect.insort(l,g2)  print 'l0 ', l[0].number_of_nodes() print 'l1' ,l[1].number_of_nodes() 

if there's way of doing that, great to.

thanks

the arbitrary order of list due fact objects ordered id (which derived ram address of object in cpython) unless provide other way define ordering.

the easy way solve problem use built-in list.sort method (or sorted function), passing key=len key function argument.

however, if want use bisect maintain sorted list, can too, class needs define rich comparison methods.

you could add these methods graph class, may easier (and cleaner) define new class wrapper.

here's simple example wraps built-in list type. defines private method, _cmp perform length-based comparisons, , rich comparison "magic" methods call _cmp. greater efficiency, rich comparison methods should defined directly, avoid calling method, using _cmp easier read (and write :) ).

import bisect  class mylist(object):     def __init__(self, data):         self.data = data      def __repr__(self):         return 'mylist({0!r})'.format(self.data)      def _cmp(self, other):         return len(self.data) - len(other.data)      #rich comparison methods     def __lt__(self, other):         return self._cmp(other) < 0      def __le__(self, other):         return self._cmp(other) <= 0      def __eq__(self, other):         return self._cmp(other) == 0      def __ne__(self, other):         return self._cmp(other) != 0      def __ge__(self, other):         return self._cmp(other) >= 0      def __gt__(self, other):         return self._cmp(other) > 0   data = (     [50, 60],     [10, 20, 30],     [1, 2, 3, 4, 5],     [5, 6],     [7, 8, 9, 10], )  blist = [] seq in data:     = mylist(seq)     bisect.insort(blist, a)     print(a)     print(blist)     print()     

output

mylist([50, 60]) [mylist([50, 60])]  mylist([10, 20, 30]) [mylist([50, 60]), mylist([10, 20, 30])]  mylist([1, 2, 3, 4, 5]) [mylist([50, 60]), mylist([10, 20, 30]), mylist([1, 2, 3, 4, 5])]  mylist([5, 6]) [mylist([50, 60]), mylist([5, 6]), mylist([10, 20, 30]), mylist([1, 2, 3, 4, 5])]  mylist([7, 8, 9, 10]) [mylist([50, 60]), mylist([5, 6]), mylist([10, 20, 30]), mylist([7, 8, 9, 10]), mylist([1, 2, 3, 4, 5])] 

you may take @ heapq: may find better purposes bisect. heapq (of course) use rich comparison methods if defined.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -