kal kal - 1 month ago 7
Python Question

Python, matching two list's elements

x= [0,2,3,5,6];
y= [64,384,1024,4096,384];


The above are two arrays I'm using. Im trying to match the elements together in a pythonic way

example:

if
xType
is 2 i want to compute a variable called
yType
to correspond to its value(position wise) in y. so i should get
y = 384
. if
xType = 3
i should get 1024.

How would i go about doing this

Answer

This is so short, even Stack Overflow did not allow me to submit such a short answer:

y[x.index(2)]

This will return element from y corresponding to the position of 2 or any other given value from within x list.

Hope it helped :)

Ps. Indeed dictionaries may be something you need. Try using them.

Comments