data = [ { 'name': 'Instagram', 'follower_count': 346, 'description': '社交媒體平臺', 'country': 'United States' }, { 'name': 'Cristiano Ronaldo', 'follower_count ': 215, 'description': 'Footballer', 'country': 'Portugal' }, { 'name': 'Ariana Grande', 'follower_count': 183, 'description': '音樂家和女演員', '國家' : '美國' }]
def dictionary_value():
對于資料中的值:回傳值["name"], value["follower_count"],value["description"], value["country"]
嗨,我是 Python 新手,我有一個關于字典和串列的問題:我希望我的函式字典 value() 將僅回傳值并按串列中的位置列印函式,例如:如果我想要選擇資料[2] 結果應該是:'Ariana Grande', 183,'Musician and actor','United States'。我找不到辦法做到這一點。
uj5u.com熱心網友回復:
使用dict.values方法:
def dictionary_value(i):
return list(data[i].values())
輸出:
print(dictionary_value(2))
# 'Ariana Grande', 183,'Musician and actress','United States'
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/375717.html
上一篇:使用字典計算串列串列中的元素
