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.
7 Respostas
+ 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))
+ 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]["%"])
+ 2
Thanks Oma Falk but I am still learning basics
+ 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))
+ 1
u can simply store in a csv and read csv with pandas.
after: again my code
+ 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
0
If we have to store it, how to store and assign in dictionary?