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

标签 python

有没有一种简单的方法可以根据条件将列表转换为二元决策?例如:

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/

相关文章:

Python + GNU 图 : dealing with missing values

python - Perl 代码序列化和反序列化变量,如 python pickle

python - 贪心算法和硬币算法?

python - Dragonfly 的模态命令

python - 使用scrapy从阿里巴巴抓取标题

Python 通过套接字传输文件

python - 使用 sympy 求解超越方程

python - 如何使用 Python 更新 HTML 文件中的值?

python - 动画阻尼振荡器

python - 连续大写字母正则表达式