Vera O
Vera O

Reputation: 99

Select specific element of list of lists

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

Answers (4)

Kaoru Gonzalez
Kaoru Gonzalez

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

Khakhar Shyam
Khakhar Shyam

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

Benyamin Karimi
Benyamin Karimi

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

em_bis_me
em_bis_me

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

Related Questions