小编eks*_*lan的帖子

如何编写 DRY Python For 循环

我有一个大麻数据集,其中有一列“效果”,我正在尝试为不包含某些效果的菌株添加一个二进制“nice_buds”列。这是代码:

nice_buds = []
undesired_effects = ["Sleepy", "Hungry", "Giggly", "Tingly", "Aroused", "Talkative"]

for row in sample["Effects"]:
    if "Sleepy" not in row and "Hungry" not in row and "Giggly" not in row and "Tingly" not in row and "Aroused" not in row and "Talkative" not in row:
        nice_buds.append(1)
    else:
        nice_buds.append(0)

sample["nice_buds"] = nice_buds
Run Code Online (Sandbox Code Playgroud)

截至目前,该undesired_effects列表什么都不做,并且代码在给我所需的输出方面工作得非常好。

我的问题是,是否有更“Pythonic”或“DRY”的方式来解决这个问题......

python for-loop dry

2
推荐指数
1
解决办法
79
查看次数

标签 统计

dry ×1

for-loop ×1

python ×1