[Python] Python - List of list with indefinite number of list and operation [closed]

Discussão em 'Python' iniciado por Stack, Maio 14, 2025.

  1. Stack

    Stack Membro Participativo

    I have a code in which the value will save in a list of list variable. Now i want to use that value to perform a simple addition.

    I'm not sure if there is a better way to code this but this is what in my mind

    market_share_division = df3['Geography'].unique()
    dollar_share = [[],[],[],[],[],[],[],[],[]]
    category= [] #depends on the user input

    i = 0
    max_count = len(category)
    while i< max_count:
    for ms_division in market_share_division:
    ms_division_df = df3[df3['Geography'] == ms_division]
    ms_product_df = ms_division_df[ms_division_df['Product'] == category]
    ms_df_4 = ms_product_df[ms_product_df['Time Period'].str.contains('L 4', na=False, case=False)]
    ms_df_4 = ms_df_4.head(1)
    if len(ms_df_4)>0:
    ms_df_row_1 = ms_df_4.iloc[0]
    dollar_share[int(ms_df_row_1['Dollar Sales'])%max_count].append(int(ms_df_row_1['Dollar Sales']))

    x = 0
    while x < max_count:
    dollar_share_A = dollar_share[[x][0]] + dollar_share[[x+1][0]] + dollar_share[[x+2][0]] #.... so on and so forth depending on the number of category
    dollar_share_B = dollar_share[[x][1]] + dollar_share[[x+1][1]] + dollar_share[[x+2][1]]
    x += 1


    So since there is an indefinite number of category, I'm not sure how should I use the List of list variable and the computation since the category could be anywhere between 1-10 items.

    Continue reading...

Compartilhe esta Página