from the following data:
restaurantes = [('Burger King', 'MA', 'Chicopee', '1284 Memorial Dr', '1020', '42.1927', '-72.5763'), ("Taco Bell", 'MA', 'Chicopee', '1606 Memorial Dr', '1020', '42.201117596', '-72.574102772'), ('Pizza Hut', 'MA', 'Easthampton', '113 Northampton St', '1027', '42.278666', '-72.671148'), ("Jack in the Box", 'MA', 'Holyoke', '285 Maple St', '1040', '42.20578', '-72.61053')]
I have managed to do the following function that returns the percentage of "x" restaurant brand in the restaurants variable:
def porcentaje_burger_king(restaurante, name):
nombres = [nombre for nombre, _, _, _, _, _, _ in restaurantes if nombre == name]
result = (len(nombres)/len(restaurantes))*100
return result
Which runs with:
print(porcentaje(restaurantes, "Burger King")) #(0.25)
The problem is that I can not create a function that returns the percentage of each brand that is in the restaurants variable. I have tried with the following function, which gives wrong data:
def porcentajes_por_nombres(restaurantes):
nombres = [nombre for nombre, _, _, _, _, _, _ in restaurantes]
porcentajes = []
indice = 0
while indice < len(nombres):
porcentaje_x = (len(nombres[indice])/len(restaurantes))*100
porcentajes.append(porcentaje_x)
indice +=1
return porcentajes
print(porcentajes_por_nombres(restaurantes)) #Devuelve [275.0, 225.0, 225.0, 375.0] en vez de [0.25, 0.25, 0.25, 0.25]
I need the result in the form of a list, so I can use the zip function with the variables names and percentages, thanks in advance.