Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python:根据条件将列表转换为二进制决策_Python - Fatal编程技术网

python:根据条件将列表转换为二进制决策

python:根据条件将列表转换为二进制决策,python,Python,是否有一种基于条件将列表转换为二进制决策的非详细方式?e、 g: patientAge = [19, 15, 13, 21, 37] # wanted output: [1, 0, 0, 1, 1] # true if >18 otherwise false # e.g. in matlab simply "patientAge>18" 理解:[在patientAge中v的值大于18] 如果使用numpy假设patientAge是numpy.array,则还可以写入patient

是否有一种基于条件将列表转换为二进制决策的非详细方式?e、 g:

patientAge = [19, 15, 13, 21, 37]

# wanted output: [1, 0, 0, 1, 1] # true if >18 otherwise false
# e.g. in matlab simply "patientAge>18"
理解:[在patientAge中v的值大于18] 如果使用numpy假设patientAge是numpy.array,则还可以写入patientAge>18并获得布尔numpy.array 理解:[在patientAge中v的值大于18] 如果使用numpy假设patientAge是numpy.array,则还可以写入patientAge>18并获得布尔numpy.array
只需使用列表:

>>> patientAge = [19, 15, 13, 21, 37]
>>> [age > 18 for age in patientAge]
[True, False, False, True, True]
如果必须有1或0:

>>> [int(age > 18) for age in patientAge]
[1, 0, 0, 1, 1]

只需使用列表:

>>> patientAge = [19, 15, 13, 21, 37]
>>> [age > 18 for age in patientAge]
[True, False, False, True, True]
如果必须有1或0:

>>> [int(age > 18) for age in patientAge]
[1, 0, 0, 1, 1]

可以将列表转换为numpy数组:


语法很熟悉,因为numpy和matplotlib当然基于Matlab。

您可以将列表转换为numpy数组:


语法很熟悉,因为numpy和matplotlib当然是基于Matlab的。

np.arraypatientAge>18.astypeint如果使用numpy,请尝试:meanfilterlambda x:x>18,而且,没有必要挑剔和比较语言。对于matlab擅长的每一件事,python擅长的有10件事。这对我处理的实际数据不起作用,因为我必须根据一个条件获得一个数组的索引,并从另一个数组中选择相应的索引:matlab语义的meanscorepatientAge>18?i、 18岁以上的人的平均分是多少?patientAge=[19,15,13,21,37]分数=[123,213,429,98,50]patientAge=np.arraypatientAge分数=np.arrayscore idx=patientAge>18 printidx printnp.meanscoreidx TypeError:“numpy.ndarray”对象不是带方括号的可调用分数[idx]索引。。。不全面感谢patientAge=[19,15,13,21,37]分数=[123,213,429,98,50]patientAge=np.arraypatientAge分数=np.arrayscore打印np.meanscore[patientAge>18]它就像一个符咒!np.arraypatientAge>18.astypeint如果您使用numpy,请尝试:meanfilterlambda x:x>18,而且,挑剔和比较语言是没有意义的。对于matlab擅长的每一件事,python擅长的有10件事。这对我处理的实际数据不起作用,因为我必须根据一个条件获得一个数组的索引,并从另一个数组中选择相应的索引:matlab语义的meanscorepatientAge>18?i、 18岁以上的人的平均分是多少?patientAge=[19,15,13,21,37]分数=[123,213,429,98,50]patientAge=np.arraypatientAge分数=np.arrayscore idx=patientAge>18 printidx printnp.meanscoreidx TypeError:“numpy.ndarray”对象不是带方括号的可调用分数[idx]索引。。。不全面感谢patientAge=[19,15,13,21,37]分数=[123,213,429,98,50]patientAge=np.arraypatientAge分数=np.arrayscore打印np.meanscore[patientAge>18]它就像一个符咒!