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:
JavaScript
x
6
1
arr = [['volcano_021', 'dusthaze-sky', 5, 1, 251, 55],
2
['volcano_021', 'rocky-mountain', 11, 75, 249, 256],
3
['opencountry_test_017', 'overcast-sky', 5, 5, 252, 119],
4
['opencountry_test_017', 'yellow-field', 4, 140, 254, 250],
5
['mountain_004', 'blue-sky', 9, 5, 246, 82]]
6
What I was trying to do is through a for loop get the following result for such a loop
JavaScript
1
4
1
'volcano_021' => ['dusthaze-sky','rocky-mountain']
2
'opencountry_test_017'=> ['overcast-sky', 'yellow-field']
3
'mountain_004' => ['blue-sky']
4
I came up with the following code…However it is not working.
I would like to do that using a list comprehension
JavaScript
1
3
1
for file in uniqueFiles:
2
print([n[1] for i,n in enumerate(arr) if n[i] == file])
3
Advertisement
Answer
You don’t need enumerate:
JavaScript
1
3
1
for file in uniqueFiles:
2
print([n[1] for n in arr if n[0] == file])
3