python - 在 Python 的 if 语句中创建值数组
问题描述
我想根据条件创建一个值数组。
data_total = [
{"modele": "DS",
"cote_actual": 10000},
{"modele": "DS",
"cote_actual": 12000},
{"modele": "204",
"cote_actual": 10000}]
for total in data_total:
model = total["modele"]
cote_actual = total["cote_actual"]
model_ds = []
if model == "DS":
model_cote = cote_actual
print(model_cote)
model_ds.append(model_cote)
print(model_ds)
我想要的输出:
"modele_ds" = [10000, 12000]
我弄乱了循环,我没有我需要的输入。我知道我必须附加或填充数组。
解决方案
[ x['cote_actual'] for x in data_total if x['modele'] == 'DS‘]
推荐阅读
- sql - 在sql调度期间获取员工的空闲时间段
- cluster-analysis - k-means 算法的第一个质心应该是“随机的”?
- java - 自定义声音不播放 Firebase 消息 Android
- android-ndk - LOCAL_CFLAGS 和 APP_CFLAGS 有什么区别?
- python - Python 将 xml/字符串数据插入 PostgreSql 数据库
- sql - 如何从 MS Access 中的 SQL 编辑器运行 vba 函数
- python - Keys.ALT 和 Keys.LEFT_ALT 在硒中不起作用?(Python)
- python - Django Paginator - 我在继承自 Paginator 的类中创建了一个函数,以及为什么我不能在前端使用这个函数
- php - 如何捕获和格式化输出嵌套格式标签?
- javascript - Elements 上的平滑滚动行为