Talha Nafees
Talha Nafees

Reputation: 11

can't understand the meaning of the line datasets[l.split()[2]].append(l)

I can't understand the following function while executing a python script the dataset is refering to a csv file while datasets is a dictionary so what do this line datasets[l.split()[2]].append(l) means, never saw this kind off operation done to a dictionary

datasets = {'normal': [], 'pneumonia': [], 'COVID-19': []}
    for l in self.dataset:
        datasets[l.split()[2]].append(l)
    self.datasets = [
        datasets['normal'] + datasets['pneumonia'],
        datasets['COVID-19'],
    ]
    print(len(self.datasets[0]), len(self.datasets[1]))

    self.on_epoch_end()

Upvotes: 1

Views: 62

Answers (2)

Rasputin
Rasputin

Reputation: 181

So I just broke it down using print statements. I agree with papke but would add that, presumably, your 'dataset' is a 'list' data structure and that that each 'element' in that list data structure contains a first name and last name (or some other identifier) and one of three designations contained in datasets. All this line -- datasets[l.split()[2]].append(l) -- does is split each element in your list into 3 strings, it takes the last of those strings to identify the 'key' it should use in the datasets dictionary. Then it adds the individuals info to the datasets dictionary.

Does this help?

datasets = {'normal': [], 'pneumonia': [], 'COVID-19': []}

dataset = ['John Doe normal', 'Abby Rose pneumonia', 'Sammy Hagar COVID-19']

for l in dataset:
    print(l)
    print(l.split()[2])
    datasets[l.split()[2]].append(l)

print(datasets['normal'])
print(datasets['pneumonia'])
print(datasets['COVID-19'])

This is the output:

John Doe normal
normal
Abby Rose pneumonia
pneumonia
Sammy Hagar COVID-19
COVID-19
['John Doe normal']
['Abby Rose pneumonia']
['Sammy Hagar COVID-19']

Upvotes: 0

pakpe
pakpe

Reputation: 5489

It splits l into a list, take the item at the second index of that list, looks for that item in the keys of dictionary datasets and then appends l to the value list associated with that key in the dictionary.

Upvotes: 1

Related Questions