Python - Query Over List Of Tuples
I have a list of tuples that contain a float, and two custom objects: ExampleList = [(10.5, Obj1, Obj2), (11.5, Obj1, Obj2), (14., Obj1, Obj2)] I then have a line of code that app
Solution 1:
Here's the cleanest way to do it (that I am aware of):
secondLargest = sorted(ExampleList)[-2][0]
if (newTuple[0] > secondLargest):
ExampleList.append(newTuple)
Although If you're looking for efficiency, look at @Jean-François Fabre 's answer
Solution 2:
max
is ruled out here, since we need the 2nd best value. sort
would work but it allocates the full sorted list in return, only to discard most of the values.
A nice way of getting the 2nd largest float is to use heapq.nlargest
in a generator comprehension (better check that input list has at least 2 elements in the general case, though)
import heapq
Obj1 = object()
Obj2 = object()
sampleList = [(10.5, Obj1, Obj2), (11.5, Obj1, Obj2), (14., Obj1, Obj2)]
second_largest = heapq.nlargest(2,(x[0] for x in sampleList))[-1] # [14.0, 11.5]
newTuple = (15,Obj1, Obj2)
if newTuple[0]>second_largest:
sampleList.append(newTuple)
Solution 3:
Since your list is already sorted, I would look at the bisect module to find the sorted insertion point. Then just calculate if that insertion meets the criteria.
Example:
import bisect
Obj1='o1'
Obj2='o2'
sample=[(10.5, Obj1, Obj2), (11.5, Obj1, Obj2), (14., Obj1, Obj2)] # needs to be sorted
newTuple = (13,Obj1, Obj2)
def my_insert(li, t):
ip=bisect.bisect_left(li, newTuple)
if len(li)-ip<2:
li.insert(ip, t)
else:
print t, 'not inserted'
Testing:
>>> my_insert(sample, newTuple); print sample
[(10.5, 'o1', 'o2'), (11.5, 'o1', 'o2'), (13, 'o1', 'o2'), (14.0, 'o1', 'o2')]
>>> my_insert(sample, newTuple); print sample
(13, 'o1', 'o2') not inserted
[(10.5, 'o1', 'o2'), (11.5, 'o1', 'o2'), (13, 'o1', 'o2'), (14.0, 'o1', 'o2')]
Post a Comment for "Python - Query Over List Of Tuples"