Reputation: 99
I have a list of lists:
[[0.01, 0.19, 0.72], [0.03, 0.27, 0.31], [0.23, 0.74, 0.09]]
I want to extract a different element from each list. So from the first list the second element, but from the second and third list a different element. I saved those in a different list;
selections = [1, 2, 0]
So the output I would like to have is:
0.19, 0.31, 0.23
Upvotes: 0
Views: 3791
Reputation: 11
I use Python Matrix
list=[[0.01,0.19,0.72],[0.03,0.27,0.31],[0.23,0.74,0.09]]
nums=list[0][1],list[1][2],list[-1][0]
print(nums)
Output
(0.19, 0.31, 0.23)
Upvotes: 1
Reputation: 499
x = [[0.01, 0.19, 0.72], [0.03, 0.27, 0.31], [0.23, 0.74, 0.09]]
selections = [1, 2, 0]
for i,j in zip(x,selections):
print(i[j])
Upvotes: 1
Reputation: 163
you can use this :
my_list=[[0.01, 0.19, 0.72], [0.03, 0.27, 0.31], [0.23, 0.74, 0.09]]
select=[1,2,0]
j=0
selector=[]
for i in select:
selector.append(my_list[j][i])
j=j+1
selector
output
[0.19, 0.31, 0.23]
Upvotes: 1
Reputation: 388
l=[[0.01, 0.19, 0.72], [0.03, 0.27, 0.31], [0.23, 0.74, 0.09]]
selections = [1, 2, 0]
for i in range(len(l)):
print(l[i][selections[i]])
Output:
0.19
0.31
0.23
Upvotes: 2