In short what I need is to get for each element from this set uniqueFiles = {volcano_021, opencountry_test_017, opencountry_test_017}
get the element of index 1 from each nested array in which the index 0 is equal to the element of uniqueFiles set which will be iterating.
For instance consider the followin list:
arr = [['volcano_021', 'dusthaze-sky', 5, 1, 251, 55], ['volcano_021', 'rocky-mountain', 11, 75, 249, 256], ['opencountry_test_017', 'overcast-sky', 5, 5, 252, 119], ['opencountry_test_017', 'yellow-field', 4, 140, 254, 250], ['mountain_004', 'blue-sky', 9, 5, 246, 82]]
What I was trying to do is through a for loop get the following result for such a loop
'volcano_021' => ['dusthaze-sky','rocky-mountain'] 'opencountry_test_017'=> ['overcast-sky', 'yellow-field'] 'mountain_004' => ['blue-sky']
I came up with the following code…However it is not working.
I would like to do that using a list comprehension
for file in uniqueFiles: print([n[1] for i,n in enumerate(arr) if n[i] == file])
Advertisement
Answer
You don’t need enumerate:
for file in uniqueFiles: print([n[1] for n in arr if n[0] == file])