我正在使用 Phython 中的字典列表,并尝试过滤掉多个键中满足特定条件的项目。我有以下清单:
data = [
{"name": "Atif", "age": 25, "city": "Delhi"},
{"name": "Shahid", "age": 30, "city": "Gujrat"},
{"name": "Soumya", "age": 25, "city": "Bihar"},
{"name": "Rounak", "age": 22, "city": "Delhi"}
]
我想过滤此列表以仅获取年龄为 25 且城市为“德里”的条目。我尝试过使用列表理解,但我很难有效地应用多个条件。谁能建议一种 Pythonic 方法来做到这一点?
filtered_data = [entry for entry in data if entry["age"] == 25]
我就是用这个方法过滤掉的。
filtered_data = [entry for entry in data if entry["age"] == 25 and entry["city"] == 'Delhi' ]