有没有一种简单的方法可以根据条件将列表转换为二元决策?例如:
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 = [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]
关于python:根据条件将列表转换为二元决策,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46363347/