Home > other >  Using only one list instead of multiple lists
Using only one list instead of multiple lists

Time:03-16

Multiple separate lists increase the code size and make it difficult for this code to be malleable, because if the number of lists changes, I will need to change several other code points.

So I'm trying to convert adding values in several different lists and creating a single list with other lists inside it.

This way I add values in four different lists ([loop_score,loop_country_tournament,loop_timegame,loop_actual_by_last]):

import pandas as pd

def perfect_option(graphic_function, etapa, placar, pais_uniqueTournament, timegame, actualbylast):

    csv_file = 'https://docs.google.com/spreadsheets/d/e/2PACX-1vS9NAPP4qfdW1AsTDevyx0RgtESRHR0JLYcU1Kw7W_V7mebkBGOWlxvyhcCzG-F_g-LCvpFBR-Wj5HF/pub?gid=0&single=true&output=csv'
    df = pd.read_csv(csv_file)
    cols = df.filter(regex=r'^pressao[1-5]$')

    if all(value>0 for value in graphic_function): # &   filtro &
        filtro = cols.gt(0).all(1)
    elif not all(value>0 for value in graphic_function) and not all(value<0 for value in graphic_function):
        filtro = cols.ne(0).all(1) & cols.lt(0).any(1) & cols.gt(0).any(1)
    elif all(value<0 for value in graphic_function):
        filtro = cols.lt(0).all(1)

    filters_list = ['placar','countrytournament','time game','actualbylast']
    

    finals_score = []
    finals_country_tournament = []
    finals_timegame = []
    finals_actual_by_last = []

    for flist in filters_list:
        unique_options_list = df.loc[(df['etapa'] == etapa) &   filtro, flist].unique()

        loop_score = []
        loop_country_tournament = []
        loop_timegame = []
        loop_actual_by_last = []

        for unique_option in unique_options_list:
            somatorio = df.loc[(df['etapa'] == etapa) &   filtro & (df[flist] == unique_option), 'PROFIT/LOSS'].sum()
            if (somatorio > 0):
                for optlist, four_list in zip(filters_list,[loop_score,loop_country_tournament,loop_timegame,loop_actual_by_last]):
                    lista_placar = list(set(df.loc[(df['etapa'] == etapa) &   filtro & (df[flist] == unique_option), optlist]))
                    four_list.append(lista_placar)


perfect_option([1,1,1,1,1],'1st half','1-0','England - League Two',38,35)

But I'm trying to stop using so many lists and use only one (loop_list = [[]]*len(filters_list)), as there are four loops, so I would always like the values of the first loop to be added to index[0], second to index[1], index[2] and finally to index[3], all separately:

import pandas as pd

def perfect_option(graphic_function, etapa, placar, pais_uniqueTournament, timegame, actualbylast):

    csv_file = 'https://docs.google.com/spreadsheets/d/e/2PACX-1vS9NAPP4qfdW1AsTDevyx0RgtESRHR0JLYcU1Kw7W_V7mebkBGOWlxvyhcCzG-F_g-LCvpFBR-Wj5HF/pub?gid=0&single=true&output=csv'
    df = pd.read_csv(csv_file)
    cols = df.filter(regex=r'^pressao[1-5]$')

    if all(value>0 for value in graphic_function): # &   filtro &
        filtro = cols.gt(0).all(1)
    elif not all(value>0 for value in graphic_function) and not all(value<0 for value in graphic_function):
        filtro = cols.ne(0).all(1) & cols.lt(0).any(1) & cols.gt(0).any(1)
    elif all(value<0 for value in graphic_function):
        filtro = cols.lt(0).all(1)

    filters_list = ['placar','countrytournament','time game','actualbylast']
    

    finals_score = []
    finals_country_tournament = []
    finals_timegame = []
    finals_actual_by_last = []

    for flist in filters_list:
        unique_options_list = df.loc[(df['etapa'] == etapa) &   filtro, flist].unique()

        loop_list = [[]]*len(filters_list)

        for unique_option in unique_options_list:
            somatorio = df.loc[(df['etapa'] == etapa) &   filtro & (df[flist] == unique_option), 'PROFIT/LOSS'].sum()
            if (somatorio > 0):
                for i,optlist in enumerate(filters_list):
                    lista_placar = list(set(df.loc[(df['etapa'] == etapa) &   filtro & (df[flist] == unique_option), optlist]))
                    loop_list[i].append(lista_placar)

        print(loop_list)


perfect_option([1,1,1,1,1],'1st half','1-0','England - League Two',38,35)

How should I proceed to correctly add the values to their proper lists within the list?

I am available to add or answer any questions if the codes are not very clear for understanding.

The expected result with the second code would be the same as if we combined all the values from the four lists of the first code, like:

[[loop_score],[loop_country_tournament],[loop_timegame],[loop_actual_by_last]]

CodePudding user response:

Sorry for taking some time, I have a bit hard time understanding the data(but managed to do so). If you do not want to have so many lists in your function, you can first return the result with a list:

def perfect_option(graphic_function, etapa, placar, pais_uniqueTournament, timegame, actualbylast):

    csv_file = 'https://docs.google.com/spreadsheets/d/e/2PACX-1vS9NAPP4qfdW1AsTDevyx0RgtESRHR0JLYcU1Kw7W_V7mebkBGOWlxvyhcCzG-F_g-LCvpFBR-Wj5HF/pub?gid=0&single=true&output=csv'
    df = pd.read_csv(csv_file)
    cols = df.filter(regex=r'^pressao[1-5]$')

    if all(value>0 for value in graphic_function): # &   filtro &
        filtro = cols.gt(0).all(1)
    elif not all(value>0 for value in graphic_function) and not all(value<0 for value in graphic_function):
        filtro = cols.ne(0).all(1) & cols.lt(0).any(1) & cols.gt(0).any(1)
    elif all(value<0 for value in graphic_function):
        filtro = cols.lt(0).all(1)

    filters_list = ['placar','countrytournament','time game','actualbylast']


    result = []

    for flist in filters_list:
        unique_options_list = df.loc[(df['etapa'] == etapa) &   filtro, flist].unique()
        
        for unique_option in unique_options_list:
            somatorio = df.loc[(df['etapa'] == etapa) &   filtro & (df[flist] == unique_option), 'PROFIT/LOSS'].sum()
            if (somatorio > 0):
                for optlist in filters_list:
                    lista_placar = list(set(df.loc[(df['etapa'] == etapa) &   filtro & (df[flist] == unique_option), optlist]))
                    result.append(lista_placar)
    
    return result

data=perfect_option([1,1,1,1,1],'1st half','1-0','England - League Two',38,35)

After that, instead of looping it, you can use list slicing to obtain 4 lists

finals_score = data[0::4]
finals_country_tournament = data[1::4]
finals_timegame = data[2::4]
finals_actual_by_last = data[3::4]

To make it easier for understanding, I put it into dataframe

df=pd.DataFrame({"Final Score":finals_score,
            "Finals Country Tournament":finals_country_tournament,
            "Finals Timegame":finals_timegame,
            "Finals Actual By Last":finals_actual_by_last})

   Final Score  ...     Finals Actual By Last
0        [0-0]  ...  [32, 36, 38, 20, 23, 31]
1        [0-2]  ...                    [8, 9]
2        [2-0]  ...                       [6]
3   [2-0, 0-0]  ...                   [6, 31]
4        [0-1]  ...                      [27]
5        [0-0]  ...                      [20]
6   [0-2, 0-0]  ...                   [9, 36]
7        [0-0]  ...                  [32, 20]
8        [0-0]  ...                      [38]
9   [0-1, 0-0]  ...                  [27, 31]
10       [0-0]  ...                      [20]
11       [0-2]  ...                       [9]
12  [0-1, 0-0]  ...                  [28, 38]
13       [0-0]  ...                      [32]
14       [0-0]  ...                      [36]
15       [0-0]  ...                      [31]
16       [0-1]  ...                      [27]
17       [0-0]  ...                      [20]
18       [0-2]  ...                       [9]
19       [0-0]  ...                      [38]
20       [0-0]  ...                      [32]
21       [0-0]  ...                      [36]

[22 rows x 4 columns]
  • Related