I've got a structure of the form:
>>> items [([[0, 1], [2, 20]], 'zz', ''), ([[1, 3], [5, 29], [50, 500]], 'a', 'b')]
The first item in each tuple is a list of ranges, and I want to make a generator that provides me the ranges in ascending order based on the starting index.
Since the range-lists are already sorted by their starting index this operation is simple: it is just a sorted merge. I'm hoping to do it with good computational efficiency, so I'm thinking that one good way to implicitly track the state of my merge is to simply pop the front off of the list of the tuple which has the smallest starting index in its range list.
I can use min()
to obtain [0, 1]
which is the first one I want, but how do I get the index of it?
I have this:
[ min (items[i][0]) for i in range(len(items)) ]
which gives me the first item in each list, which I can then min()
over somehow, but it fails once any of the lists becomes empty, and also it's not clear how to get the index to use pop()
with without looking it back up in the list.
To summarize: Want to build generator that returns for me:
([0,1], 'zz', '') ([1,3], 'a', 'b') ([2,20], 'zz', '') ([5,29], 'a', 'b') ([50,500], 'a', 'b')
Or even more efficiently, I only need this data:
[0, 1, 0, 1, 1]
(the indices of the tuples i want to take the front item of)
Python provides different inbuilt function. min() is used for find out minimum value in an array, max() is used for find out maximum value in an array. index() is used for finding the index of the element.
The python has a built-in function of min() which returns the minimum value in the list and index() which returns the index of the element. These two functions can be used to find the index of a minimum element in a single line code.
Use the enumerate() function to find out the index of the maximum value in a list. Use the numpy. argmax() function of the NumPy library to find out the index of the maximum value in a list.
from operator import itemgetter index, element = max(enumerate(items), key=itemgetter(1))
Return the index of the biggest element in items
and the element itself.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With