Python Array - Highest list value

Asked

Viewed 85 times

-2

I’m having trouble solving an array exercise in Python, can help?

Description: A store with 4 branches wants to register the daily sale of each branch for 1 week period. Create an array to store the sales. Write a feature that tells you which day of the week had the highest sales volume (including the 4 branches) and another that returns the number of the branch that had the highest weekly sales.

What I’ve done so far:

#Inserção dos valores para as matrizes 1x7, ja que são valores unitarios por dias da semana
loja_1=[[0,0,0,0,0,0,0]]
loja_2=[[0,0,0,0,0,0,0]]
loja_3=[[0,0,0,0,0,0,0]]
loja_4=[[0,0,0,0,0,0,0]]


def space():
    print('===========================================')

#Laço para inserção de valores da filial 1
for l in range(1):
    for c in range(0,7):
        loja_1[l][c]=int(input('\nDigite o valor das vendas por dia da semana, para a filial 1: '))

#Laço para formatar os valores da matriz da filial 1
for l in range(1):
    for c in range(0,7):
        print([loja_1[l][c]],end='')
    print()

space()

#Laço para inserção de valores da filial 2
for l in range(1):
    for c in range(0,7):
        loja_2[l][c]=int(input('\nDigite o valor das vendas por dia da semana, para a filial 2: '))
#Laço para formatar os valores da matriz da filial 2
for l in range(1):
    for c in range(0,7):
        print([loja_2[l][c]],end='')
    print()

space()

#Laço para inserção de valores da filial 3
for l in range(1):
    for c in range(0,7):
        loja_3[l][c]=int(input('\nDigite o valor das vendas por dia da semana, para a filial 3: '))
#Laço para formatar os valores da matriz da filial 3
for l in range(1):
    for c in range(0,7):
        print([loja_3[l][c]],end='')
    print()

space()

#Laço para inserção de valores da filial 4
for l in range(1):
    for c in range(0,7):
        loja_4[l][c]=int(input('\nDigite o valor das vendas por dia da semana, para a filial 4: '))
#Laço para formatar os valores da matriz da filial 4
for l in range(1):
    for c in range(0,7):
        print([loja_4[l][c]],end='')
    print()

I stuck in the part: "Write a function that tells you which day of the week had the highest sales volume (including the 4 branches) and another that returns the number of the branch that had the highest weekly sales."

When I try to extract as many as loja_1, error stating that list is not eternal. I tried to convert list for int, used import heapq, used max(loja_1), but I can’t. Some help?

1 answer

0

First of all, you can create a single matrix to store all four branches. This will make it easier to work with the value of branch sales since they will not now be separated into variables. In addition, it will be possible to create more branches without creating new variables.

To create this matrix, you must join all affiliates in the same list in this way:

lojas = []

for loja in range(5):

    vendas = []

    for dia in range(7):
        venda = int(input(f"Digite o valor de vendas da {loja + 1}ª loja no {dia + 1}º dia: "))
        vendas.append(venda)

    lojas.append(vendas)

With the code above, the matrix lojas should be in the following format (example values):

[[20, 13, 2, 5, 6, 2, 4], 
[5, 7, 1, 8, 9, 11, 33], 
[4, 2, 21, 3, 4, 15, 3], 
[17, 34, 2, 1, 0, 2, 3], 
[6, 7, 4, 3, 45, 2, 2]]

To get the day of the week with more sales, just go through the days and add up the sales of all stores on that particular day. After that just compare on which day you sold the most.

To make this sum, you can traverse the rows of the matrix obtaining the value of the column referring to the day within a List Comprehension and sum the values of the returned list with sum():

melhor_dia = [0, 0] # [vendas, dia_da_semana]

for dia in range(7):
    vendas = sum([loja[dia] for loja in lojas])

    if vendas > melhor_dia[0]:
        melhor_dia = [vendas, dia] 

To get the store with the highest number of sales is much easier because just add all the sales of each store with the function sum() and compare results. See the code below:

melhor_loja = [0, 0] # [vendas, loja]

for loja in range(len(lojas)):
    vendas = sum(lojas[loja])

    if vendas > melhor_loja[0]:
        melhor_loja = [vendas, loja]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.