0

How to assign key value from below list to dict? And and sum all. Below key values are assigned manually. How take it from list?

data = [ [23, 11, 5, 14], [8, 32, 20, 5] ] color = input() data = { 'brown' : 31, 'blue' : 43, 'green' : 25, 'black' : 19 } print(int(data[color]/118*100)) The columns in data[]are the eye colors, in the following order: brown, blue, green, black: Our program needs to take eye color as input and output the percentage of people with that eye color in the room. Sample Input: blue Sample Output: 36 Explanation: There are 11+32=43 people with blue eyes, which is 36% of the total.

30th Jun 2021, 4:36 PM
Zahed Shaikh
Zahed Shaikh - avatar
7 ответов
+ 3
#Try this data = [ [23, 11, 5, 14], [8, 32, 20, 5] ] color = input() color_dict = {"brown":0, "blue":1, "green":2, "black":3} print(int((data[0][color_dict[color]]+data[1][color_dict[color]])/(sum(data[0])+sum(data[1]))*100))
30th Jun 2021, 5:06 PM
Simba
Simba - avatar
+ 2
Hi, perfect for pandas import pandas as pd import numpy as np df = pd.DataFrame(np.transpose([[23,11,5,14],[8,32,20,5]]), index = ["brown","blue","green","black"]) mysum = df[0] + df[1] df["sum"]=mysum perc = df['sum'] / sum(df['sum'])*100 df["%"] = perc color = input("Please enter a color") while not color in df.index: color = input("Please enter a valid color") print(df.loc[color]["%"])
30th Jun 2021, 5:26 PM
Oma Falk
Oma Falk - avatar
+ 2
Thanks Oma Falk but I am still learning basics
30th Jun 2021, 6:10 PM
Zahed Shaikh
Zahed Shaikh - avatar
+ 1
I think we don't need to store the data in list since they are also stored in Dictionary ''' data = [ [23, 11, 5, 14], [8, 32, 20, 5] ]''' color = input() data = { 'brown' : 31, 'blue' : 43, 'green' : 25, 'black' : 19 } print(int(data[color]/118*100))
30th Jun 2021, 4:52 PM
Simba
Simba - avatar
+ 1
u can simply store in a csv and read csv with pandas. after: again my code
30th Jun 2021, 6:05 PM
Oma Falk
Oma Falk - avatar
+ 1
We can store eye colours in a tuple then use dictionary comprehension. Index of element to sum from the two inner lists can be obtained by the use of enumerate() function. https://code.sololearn.com/cwZJ3NvX46Tq/?ref=app
30th Jun 2021, 7:32 PM
Ipang
0
If we have to store it, how to store and assign in dictionary?
30th Jun 2021, 4:53 PM
Zahed Shaikh
Zahed Shaikh - avatar