python - python中的if-for-else嵌套集合理解
问题描述
我正在尝试将以下嵌套条件转换为设置理解,但无法使其正常工作。
processed = set()
if isinstance(statements, list):
for action in statements:
processed.add(action)
else:
processed.add(statements)
我尝试了以下但看起来我犯了一个错误
processed = {action for action in statements if isinstance(statements, list) else statements}
编辑:哪里statements
可以是列表或字符串。
解决方案
您需要if
集合理解之外的语句,如本else
例所示,statements
它不是可迭代的
processed = {action for action in statements} if isinstance(statements, list) else {statements}
推荐阅读
- plot - 使用动态线宽时无法绘图
- python - Top3 K 标签的精度和召回率
- php - 流明/laravel 采摘不适合关系
- python - Discord.py 公告频道的代码是什么?
- c++ - MSVC Address Sanitizer - 有什么理由在 Release 版本中使用它?
- reactjs - 通过表单引用在表单项上设置禁用
- linux - 有什么方法可以知道哪些环境变量正在尝试读取 linux 程序?
- javascript - Electron:退出渲染器但不是主进程
- javascript - 如何在同一页面上加载 reCAPTCHA 分数和复选框?
- weblogic12c - 是否可以在 Apple M1 计算机上安装 Weblogic 12.2.1.4?