简体   繁体   English

数据操作/索引python vs R

[英]data manipulation/ indexing python vs R

I am inexperienced with python and am unsure what i should search for this particular task. 我对python没有经验,不确定我应该搜索什么特定任务。 I am trying to find a way to index a list much like i would index a vector in R: 我正在尝试找到一种索引列表的方法,就像我在R中对向量进行索引一样:

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 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??? 请注意,即使第一个值为1,对于所有三个值均为False。

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??? 因此,当涉及到python时,我只是在寻找一种索引python列表的方法,就像我可以使用R一样???

What about : 关于什么 :

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

an alternative: 替代:

map(lambda x: x == 1, list_a)
#[True, False, False]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM