Using NumPy to Find Median of Second Element of List of Tuples
问题 Let's say I have a list of tuples, as follows: list = [(a,1), (b,3), (c,5)] My goal is to obtain the first element of the median of the list of tuples, using the tuples' second element. In the above case, I would want an output of b, as the median is 3. I tried using NumPy with the following code, to no avail: import numpy as np list = [('a',1), ('b',3), ('c',5)] np.median(list, key=lambda x:x[1]) 回答1: You could calculate the median like this: np.median(dict(list).values()) # in Python 2.7;