是这个list: [{ text: '北京市', id: '0', disabled:false }, { text: '天津市', id: '1', disabled:false }, { text: '河北省', id: '2', disabled:false }, { text: '山西省', id: '3', disabled:false }, ]
时间: 2023-12-29 09:03:49 浏览: 30
感谢您提供的信息!根据您的要求,我们需要在给定的列表中查找与 ['0', '1', '2'] 中的元素具有相同 ID 的对象。下面是一个示例代码来实现这个功能:
```python
# 给定的列表
lst = [{
'text': '北京市',
'id': '0',
'disabled': False
}, {
'text': '天津市',
'id': '1',
'disabled': False
}, {
'text': '河北省',
'id': '2',
'disabled': False
}, {
'text': '山西省',
'id': '3',
'disabled': False
}]
# 要查找的元素
target_ids = ['0', '1', '2']
# 使用列表推导式查找具有相同 ID 的对象
result = [item for item in lst if item['id'] in target_ids]
# 打印结果
print(result)
```
运行以上代码,您将得到一个包含与 ['0', '1', '2'] 中的元素具有相同 ID 的对象的列表。在给定的示例数据中,结果将是:
```
[{
'text': '北京市',
'id': '0',
'disabled': False
}, {
'text': '天津市',
'id': '1',
'disabled': False
}, {
'text': '河北省',
'id': '2',
'disabled': False
}]
```
希望这能帮到您!如果有任何进一步的问题,请随时向我提问。