擅长:python、mysql、java
<p>您必须遍历字典列表并搜索具有给定<code>id_number</code>的字典。一旦找到它,就可以打印它的其余数据并中断,假设<code>id_number</code>是唯一的。</p>
<pre><code>data = [
{
"id_number": "SA4784",
"name": "Mark",
"birthdate": None
},
{
"id_number": "V410Z8",
"name": "Vincent",
"birthdate": "15/02/1989"
},
{
"id_number": "CZ1094",
"name": "Paul",
"birthdate": "27/09/1994"
}
]
for i in data:
if i['id_number'] == 'V410Z8':
print(i['birthdate'])
print(i['name'])
break
</code></pre>
<p>如果您可以控制数据结构,一种更有效的方法是使用<code>id_number</code>作为键(同样,假设<code>id_number</code>是唯一的):</p>
<pre><code>data = { "SA4784" : {"name": "Mark", "birthdate": None},
"V410Z8" : { "name": "Vincent", "birthdate": "15/02/1989"},
"CZ1094" : {"name": "Paul", "birthdate": "27/09/1994"}
}
</code></pre>
<p>然后,您只需尝试直接访问它:</p>
<pre><code>try:
print(data["V410Z8"]["name"])
except KeyError:
print("ID doesn't exist")
>> "Vincent"
</code></pre>