
id_number。一旦找到它,您就可以打印其其余数据并中断(假设
id_number是唯一的)。
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如果您可以控制数据结构,则更有效的方法是使用
id_number键作为键(同样,假设
id_number是唯一的):
data = { "SA4784" : {"name": "Mark", "birthdate": None}, "V410Z8" : { "name": "Vincent", "birthdate": "15/02/1989"}, "CZ1094" : {"name": "Paul", "birthdate": "27/09/1994"} }然后,您需要做的就是尝试直接访问它:
try: print(data["V410Z8"]["name"])except KeyError: print("ID doesn't exist")>> "Vincent"欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)