pythonlistwhile-loop

Lists within repeating structure


I need to write the repeating structure data into named lists. The name of the list needs to be determined in the variable i. I have the following input data:

Nome Local Hora
Thiago Cantina 08:01
Ana Cantina 08:05
Thiago Cantina 08:10
Thiago Sala01 08:50
Jose Sala01 09:03
Thiago Sala01 09:30
Jose Sala01 09:45
Jose Cantina 09:49
Ana Biblioteca 11:07

Code:

#Input
list_entry=[['Thiago', 'Cantina', '08:01:00'], ['Ana', 'Cantina', '08:05:00'], ['Thiago', 'Cantina', '08:10:00'], ['Thiago', 'Sala01', '08:50:00'], ['Jose', 'Sala01','09:03:00'], ['Thiago', 'Sala01', '09:30:00'], ['Jose', 'Sala01', '09:45:00'], ['Jose', 'Cantina', '09:49:00'], ['Ana', 'Biblioteca', '11:07:00']]


i=1
lista=[]
    while i <= 4:#repeticacao1
        y=0
        inicio, fim = intervalo[i] #interval is a dictionary with time period every 1 hour
        #The data of the block below must be written in a list with the name list1. After exiting While(#repeticacao2) and the i is incremented, the data must be written to a list named list2 and so on.
        while y < 6: #repeticacao2 - Stop condition is <6 due to the number of input registers
            registro2 = df_registro['Hora'].dt.time[y]
            if inicio <= registro2 < fim: # checks if time is between the start and end of the range
                print(f'O registro esta dentro do intervalo {inicio:%H:%M} às {fim:%H:%M}')
                #Create a list with Name and Location datal
                lista.append([df_registro['Nome'][y],df_registro['Local'][y]])
            y += 1
        i += 1

Result:

[['Thiago', 'Cantina'], ['Ana', 'Cantina'], ['Thiago', 'Cantina'], ['Thiago', 'Sala01'], ['Jose', 'Sala01'], ['Thiago', 'Sala01'], ['Jose', 'Sala01'], ['Jose', 'Cantina'], ['Ana', 'Biblioteca']]

The result is a list of all records. However, in this way, the time intervals are being mixed.

The expected result would be for separate time intervals. Thus:

list1 = [['Thiago', 'Cantina'], ['Ana', 'Cantina'], ['Thiago', 'Cantina'], ['Thiago', 'Sala01']]

list2 = [['Jose', 'Sala01'], ['Thiago', 'Sala01'], ['Jose', 'Sala01'], ['Jose', 'Cantina']]

list3 = []

list4 = ['Ana', 'Biblioteca']

I tried to create the list with list[i] and then with list[i].append but it doesn't work. How could it be done?


Solution

  • Code:

    df["Hora"] = pd.to_datetime(df["Hora"])
    d = df.set_index('Hora').resample('1H', label='right').sum()  #Inorder to get the Hourly index including 10 o'clock
    
    #Here I am separating the sum string by capital latter and converting them
    #into list by using .split()
    
    import re
    d['Nome'] = d['Nome'].apply(lambda x: re.sub(r"\B([A-Z])", r" \1", str(x)))
    d['Local'] = d['Local'].apply(lambda x: re.sub(r"\B([A-Z])", r" \1", str(x)))
    
    #lastly zipping both column list values and define a new column
    d['List'] = d.apply(lambda x: [list(a) for a in zip(x.Nome.split(' '), x.Local.split(' '))], axis=1)
    

    Print:

    d['List'][0]    #Lets say the List1
    

    Output:

    [['Thiago', 'Cantina'],
     ['Ana', 'Cantina'],
     ['Thiago', 'Cantina'],
     ['Thiago', 'Sala01']]
    

    And to get all list into one:

    [[y for y in x if '0' not in y] for x in list(d.List)] #Here Looping just to avoid '0' at 10 o'clock
    

    Output:

    [[['Thiago', 'Cantina'],
      ['Ana', 'Cantina'],
      ['Thiago', 'Cantina'],
      ['Thiago', 'Sala01']],
     [['Jose', 'Sala01'],
      ['Thiago', 'Sala01'],
      ['Jose', 'Sala01'],
      ['Jose', 'Cantina']],
     [],
     [['Ana', 'Biblioteca']]]
    

    UPDATE:

    In order to lowercase the all list element:

    Option 1: by add lambda function

    [[list((map(lambda x: x.lower(), y))) for y in x if '0' not in y] for x in list(d.List)]
    

    Option 2: by for loop

    [[[x.lower() for x in y] for y in x if '0' not in y] for x in list(d.List)]
    

    Output:

    [[['thiago', 'cantina'],
      ['ana', 'cantina'],
      ['thiago', 'cantina'],
      ['thiago', 'sala01']],
     [['jose', 'sala01'],
      ['thiago', 'sala01'],
      ['jose', 'sala01'],
      ['jose', 'cantina']],
     [],
     [['ana', 'biblioteca']]]