johnny utah johnny utah - 2 months ago 17
R Question

data manipulation/ indexing python vs R

I am inexperienced with python and am unsure what i should search for this particular task. I am trying to find a way to index a list much like i would index a vector in R:

R



vec=c(1,2,3)

> vec==1

[1] TRUE FALSE FALSE


python



>>> list_a=[1,2,3]
>>> list_a==1
False


separate attempt in python



for i in list_a:
... i==1
...
False
False
False


Notice above that it is False for all three values even though the first value is 1???

And yet



>>> for i in list_a:
... if i==1:
... print('hello')
...
hello


Hence when it comes to python I am just looking for a way to index the the python list in the same way as i can I'm R???

Answer

What about :

>>> [x == 1 for x in list_a]
[True, False, False]
Comments