Home > Blockchain >  Lists within repeating structure
Lists within repeating structure

Time:09-07

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?

CodePudding user response:

Do you have to use a while loop here? It seems like a good case for comprehensions.

Something along the lines of

hourly_list = [entry for entry in entry_list if interval_start <= entry.time < interval_end]

would give you the list of entries within a given interval (I haven't quite used the data structure you're working with but the method should work).

CodePudding user response:

Creating a separate variable for each location is counter-productive. Instead, consider using a dictionary keyed by location:

{k: list(g['Nome']) for k, g in df.groupby('Local')}
  • Related