How to find max average of values by converting list of tuples to dictionary?

2024/10/6 4:06:36

I want to take average for all players with same name. I wrote following code. it's showing index error what's the issue?

Input: l = [('Kohli', 73), ('Ashwin', 33), ('Kohli', 7), ('Pujara', 122),('Ashwin', 90)]

Output: l = [('Kohli', 40), ('Ashwin', 61.5), ('Pujara', 122)]

t=0
l2=[]
for i in range(len(l)):for j in range(len(l)):if j < len(l) - 1 :if l[i][0] == l[j][0]:l2[t][0] = l[j][0]l2[t][1] = (l[j][1] + l[j+1][1]) / 2t = t + 1
Answer

One way with the help of default dict

from collections import defaultdict
data_dict = defaultdict(list)
l = [('Kohli', 73), ('Ashwin', 33), ('Kohli', 7), ('Pujara', 122), ('Ashwin', 90)]for k, v in l:data_dict[k].append(v)
data_dict = dict(data_dict)
# {'Pujara': [122], 'Ashwin': [33, 90], 'Kohli': [73, 7]}for k,v in data_dict.items():data_dict[k] =  sum(v)/len(v)# {'Ashwin': 61.5, 'Kohli': 40.0, 'Pujara': 122.0}

To convert the dict to list of tuples you can use zip i.e

list(zip(data_dict.keys(),data_dict.values()))#[('Ashwin', 61.5), ('Pujara', 122.0), ('Kohli', 40.0)]

To find the maximum value you can use max i.e

max(data_dict.values()) #122

To get the key you can use

[i for i,j in data_dict.items() if j == max(data_dict.values())] # ['Pujara']
https://en.xdnf.cn/q/118990.html

Related Q&A

Cannot pass returned values from function to another function in python

My goal is to have a small program which checks if a customer is approved for a bank loan. It requires the customer to earn > 30k per year and to have atleast 2 years of experience on his/her curren…

What is the difference here that prevents this from working?

Im reading a list of customer names and using each to find an element. Before reading the list, I make can confirm this works when I hard-code the name,datarow = driver.find_element_by_xpath("//sp…

How can I extract numbers based on context of the sentence in python?

I tried using regular expressions but it doesnt do it with any context Examples:: "250 kg Oranges for Sale" "I want to sell 100kg of Onions at 100 per kg"

Chart with secondary y-axis and x-axis as dates

Im trying to create a chart in openpyxl with a secondary y-axis and an DateAxis for the x-values.For this MWE, Ive adapted the secondary axis example with the DateAxis example.from datetime import date…

Env var is defined on docker but returns None

I am working on a docker image I created using firesh/nginx-lua (The Linux distribution is Alpine): FROM firesh/nginx-luaCOPY ./nginx.conf /etc/nginx COPY ./handler.lua /etc/nginx/ COPY ./env_var_echo.…

No Browser is Open issue is coming when running the Robot framework script

I have created Test.py file which has some function in it and using those function names as Keywords in sample.robot file. Ex: - Test.pydef Launch_URL(url):driver.get(url)def article(publication): do…

How to run python file in Tkinter

Im a beginner in Python, hence the question. i would like to run a python file (smileA.py) in Tkinter. How would i start? I do not wish for it to run when clicking a button, but the file to run automa…

Discord.py Cogs “Command [ ] is not found”

I am recoding my discord.py bot using cogs as it wasnt very nice before. I tried this code: import discord import os import keep_alive from discord.ext import commands from discord.ext.commands import …

Binomial distribution CDF using scipy.stats.binom.cdf [duplicate]

This question already has answers here:Alternatives for returning multiple values from a Python function [closed](14 answers)Closed 2 years ago.I wrote below code to use binomial distribution CDF (by u…

How to get Cartesian product of two iterables when one of them is infinite

Lets say I have two iterables, one finite and one infinite:import itertoolsteams = [A, B, C] steps = itertools.count(0, 100)I was wondering if I can avoid the nested for loop and use one of the infinit…