Python - Get max value in a list of dict

2024/9/20 14:24:49

I have a dataset with this structure :

In[17]: allIndices
Out[17]: 
[{0: 0, 1: 1.4589, 4: 2.4879},
{0: 1.4589, 1: 0, 2: 2.1547},
{1: 2.1547, 2: 0, 3: 4.2114},
{2: 4.2114, 3: 0},
{0: 2.4879, 4: 0}]

I'd like to identify the highest value inside the list of dict. I do as follow :

def findMax(data):possiblemax = []for i in range(len(data)):temp = list(data[i].values())tempmax = max(temp)possiblemax.append(tempmax)maxValue = max(possiblemax)return maxValue

It works but I work with a very large dataset and it's a little bit slow. I was wondering of a better and faster way of doing this.

Thanks a lot

Answer

I think the one liner version is succinct without being unreadable:

my_list = [{0: 0, 1: 1.4589, 4: 2.4879},{0: 1.4589, 1: 0, 2: 2.1547},{1: 2.1547, 2: 0, 3: 4.2114},{2: 4.2114, 3: 0},{0: 2.4879, 4: 0}]
print(max(max(part.values()) for part in my_list))

Python 3 code. Use dict.itervalues() for Python 2.

https://en.xdnf.cn/q/72260.html

Related Q&A

Rescaling axis in Matplotlib imshow under unique function call

I have written a function module that takes the argument of two variables. To plot, I hadx, y = pylab.ogrid[0.3:0.9:0.1, 0.:3.5:.5] z = np.zeros(shape=(np.shape(x)[0], np.shape(y)[1]))for i in range(le…

f2py array valued functions

Do recent versions of f2py support wrapping array-valued fortran functions? In some ancient documentation this wasnt supported. How about it now?Lets for example save the following function as func.f…

Unique strings in a pandas dataframe

I have following sample DataFrame d consisting of two columns col1 and col2. I would like to find the list of unique names for the whole DataFrame d. d = {col1:[Pat, Joseph, Tony, Hoffman, Miriam, Good…

finding index of multiple items in a list

I have a list myList = ["what is your name", "Hi, how are you","What about you", "How about a coffee", "How are you"]Now I want to search index of all …

Debugging asyncio code in PyCharm causes absolutely crazy unrepeatable errors

In my project that based on asyncio and asyncio tcp connections that debugs with PyCharm debugger I got very and very very absurd errors.If I put breakpoint on code after running, the breakpoint never …

how to generate pie chart using dict_values in Python 3.4?

I wanted the frequency of numbers in a list which I got using Counter library. Also, I got the keys and values using keys = Counter(list).keys() and values = Counter(list).values() respectively, where …

How can I make start_url in scrapy to consume from a message queue?

I am building a scrapy project in which I have multiple spiders( A spider for each domain). Now, the urls to be scraped come dynamically from a user given query. so basically I do not need to do broad…

Pip install results in this error cl.exe failed with exit code 2

Ive read all of the other questions on this error and frustratingly enough, none give a solution that works. If I run pip install sentencepiece in the cmd line, it gives me the following output.src/sen…

how to communicate two separate python processes?

I have two python programs and I want to communicate them. Both of them are system services and none of them is forked by parent process.Is there any way to do this without using sockets? (eg by cra…

Why does one use of iloc() give a SettingWithCopyWarning, but the other doesnt?

Inside a method from a class i use this statement:self.__datacontainer.iloc[-1][c] = valueDoing this i get a "SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a Data…