Question

I use this implementation of SortedCollection.

>>> a = SortedCollection(key=itemgetter(1))
>>> a.insert_right(('A',5))
>>> a.insert_right(('B',3))
>>> a.insert_right(('C',7))
>>> a
SortedCollection([('B', 3), ('A', 5), ('C', 7)], key=<operator.itemgetter object at 0x028C99B0>)

What would be the syntax of finding the index of the item with 'A'?
Notice that 'A' is not the sorting key I chose.

Here's a failed way to do it:

>>> a.find(lambda item: item[0]=='a')

Traceback (most recent call last):
  File "<pyshell#32>", line 1, in <module>
    a.find(k=lambda item: item[0]=='a')
  File "C:/dev/sorted_collection.py", line 167, in find
    raise ValueError('No item found with key equal to: %r' % (k,))
ValueError: No item found with key equal to: <function <lambda> at 0x028DB270>

No correct solution

OTHER TIPS

Tested:

[x[0] for x in a].index('A')

SortedCollection behaves much like a list, so it's actually the same syntax as searching in

[('B', 3), ('A', 5), ('C', 7)]
a = SortedCollection(key=itemgetter(1))
a.insert_right(('A',5))
a.insert_right(('B',3))
a.insert_right(('C',7))

print [y[0] for y in a].index('B')

Result:

0
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top