如何使用 for 循环从一个键中获取 return 值,该键在列表中存储的所有字典中都有特征?
How do I use a for loop to return values from a key that features in all the dictionaries stored in a list?
如何使用 for 循环 return 存储在此列表中的词典中的所有 ssn 值:
patlist = [{'mrn': 7182, 'birthyear': 1989, 'ssn': '999-76-5071', 'fname': 'Edgar', 'lname': 'Hermann', 'zip': 72034},
{'mrn': 7491, 'birthyear': 2018, 'ssn': '999-92-3312', 'fname': 'Dionna', 'lname': 'Heathcote', 'zip': 72411},
{'mrn': 7052, 'birthyear': 2013, 'ssn': '999-80-4418', 'fname': 'Eleni', 'lname': 'Gusikowski', 'zip': 72736},
{'mrn': 4851, 'birthyear': 2011, 'ssn': '999-51-6410', 'fname': 'Ricardo', 'lname': 'Mills', 'zip': 72046},
{'mrn': 1744, 'birthyear': 1969, 'ssn': '999-72-5953', 'fname': 'Michelle', 'lname': 'Breitenberg', 'zip': 72472}]```
这是我的尝试之一:
for value in patlist['ssn']:
print['ssn']
感谢您的帮助。
由于 patlist
是字典列表,所以 value
实际上是字典。所以你可以做 value['ssn']
来打印 ssn
键的值。
patlist = [{'mrn': 7182, 'birthyear': 1989, 'ssn': '999-76-5071', 'fname': 'Edgar', 'lname': 'Hermann', 'zip': 72034},
{'mrn': 7491, 'birthyear': 2018, 'ssn': '999-92-3312', 'fname': 'Dionna', 'lname': 'Heathcote', 'zip': 72411},
{'mrn': 7052, 'birthyear': 2013, 'ssn': '999-80-4418', 'fname': 'Eleni', 'lname': 'Gusikowski', 'zip': 72736},
{'mrn': 4851, 'birthyear': 2011, 'ssn': '999-51-6410', 'fname': 'Ricardo', 'lname': 'Mills', 'zip': 72046},
{'mrn': 1744, 'birthyear': 1969, 'ssn': '999-72-5953', 'fname': 'Michelle', 'lname': 'Breitenberg', 'zip': 72472}]
for value in patlist:
print(value['ssn'])
如何使用 for 循环 return 存储在此列表中的词典中的所有 ssn 值:
patlist = [{'mrn': 7182, 'birthyear': 1989, 'ssn': '999-76-5071', 'fname': 'Edgar', 'lname': 'Hermann', 'zip': 72034},
{'mrn': 7491, 'birthyear': 2018, 'ssn': '999-92-3312', 'fname': 'Dionna', 'lname': 'Heathcote', 'zip': 72411},
{'mrn': 7052, 'birthyear': 2013, 'ssn': '999-80-4418', 'fname': 'Eleni', 'lname': 'Gusikowski', 'zip': 72736},
{'mrn': 4851, 'birthyear': 2011, 'ssn': '999-51-6410', 'fname': 'Ricardo', 'lname': 'Mills', 'zip': 72046},
{'mrn': 1744, 'birthyear': 1969, 'ssn': '999-72-5953', 'fname': 'Michelle', 'lname': 'Breitenberg', 'zip': 72472}]```
这是我的尝试之一:
for value in patlist['ssn']:
print['ssn']
感谢您的帮助。
由于 patlist
是字典列表,所以 value
实际上是字典。所以你可以做 value['ssn']
来打印 ssn
键的值。
patlist = [{'mrn': 7182, 'birthyear': 1989, 'ssn': '999-76-5071', 'fname': 'Edgar', 'lname': 'Hermann', 'zip': 72034},
{'mrn': 7491, 'birthyear': 2018, 'ssn': '999-92-3312', 'fname': 'Dionna', 'lname': 'Heathcote', 'zip': 72411},
{'mrn': 7052, 'birthyear': 2013, 'ssn': '999-80-4418', 'fname': 'Eleni', 'lname': 'Gusikowski', 'zip': 72736},
{'mrn': 4851, 'birthyear': 2011, 'ssn': '999-51-6410', 'fname': 'Ricardo', 'lname': 'Mills', 'zip': 72046},
{'mrn': 1744, 'birthyear': 1969, 'ssn': '999-72-5953', 'fname': 'Michelle', 'lname': 'Breitenberg', 'zip': 72472}]
for value in patlist:
print(value['ssn'])