我有一個類似于以下內容的字典串列:
points = [
{"callsign": "A001", "daily_points":10, "number":" 447774076621"},
{"callsign": "A002", "daily_points":5, "number":" 447958708481"}
]
我正在嘗試遍歷串列以列印如下訊息:“嗨 A001,您今天收到了 10 分”
我嘗試使用以下代碼:
for callsign, daily_points in points.items():
print(f"Hi {callsign} you recieved {daily_points} points today.")
但它回傳: AttributeError: 'list' object has no attribute 'items'
我究竟做錯了什么?
uj5u.com熱心網友回復:
points是一個串列,而不是字典。您必須遍歷串列中的每個元素,然后閱讀字典。代碼看起來像這樣:
for point in points:
print(f"Hi {point['callsign']} you received {point['daily_points']} points today.")
uj5u.com熱心網友回復:
您可以使用str.format:
for point in points:
print("Hi {callsign} you recieved {daily_points} points today.".format(**point))
此外,由于在 python 3.6 dict 的保留順序中,您可以保留大部分代碼,只需替換points.items()為map(dict.values, points):
for callsign, daily_points, *_ in map(dict.values, points):
print(f"Hi {callsign} you recieved {daily_points} points today.")
uj5u.com熱心網友回復:
變數“points”是字典串列,而不是字典本身。要遍歷串列中的每個字典,您可以:
points = [
{"callsign": "A001", "daily_points":10, "number":" 447774076621"},
{"callsign": "A002", "daily_points":5, "number":" 447958708481"}
]
for point in points:
print(f"Hi {point['callsign']} you recieved {point['daily_points']} points today.")
這將遍歷串列,并列印出每個字典中的資訊。
uj5u.com熱心網友回復:
你可以試試這個:
for point in points:
callsign, daily_points, number = point['callsign'], point['daily_points'], point['number']
print(f"Hi {callsign} you recieved {daily_points} points today.")
uj5u.com熱心網友回復:
您可以遍歷每個記錄points并執行此操作。
for i in range(len(points)):
print(f"Hi {points[i]['callsign']} you recieved {points[i]['daily_points']} points today.")
輸出
Hi A001 you recieved 10 points today.
Hi A002 you recieved 5 points today.
uj5u.com熱心網友回復:
最簡單的解決方案如下所示:
for person in points:
print(f"Hi {person['callsign']} you recieved {person['daily_points']} points today.")
您的解決方案是錯誤的,因為串列沒有方法 items(),并且它無法像這樣解壓縮字典鍵。
uj5u.com熱心網友回復:
問題是您正在迭代points,這是一個串列并且沒有items屬性。
一種方法是直接使用.format和傳遞字典:
points = [
{"callsign": "A001", "daily_points": 10, "number": " 447774076621"},
{"callsign": "A002", "daily_points": 5, "number": " 447958708481"}
]
for d in points:
print("Hi {callsign} you received {daily_points} points today.".format(**d))
輸出
Hi A001 you received 10 points today.
Hi A002 you received 5 points today.
uj5u.com熱心網友回復:
您需要使用密鑰訪問該值
for user in points:
print(f"Hi {user['callsign']} you recieved {user['daily_points']} points today.")
uj5u.com熱心網友回復:
問題是 items() 方法僅適用于串列,不適用于字典。但是,您可以嘗試以下操作:
for dictionary in points:
print(f"Hi {dictionary['callsign']} you recieved {dictionary['daily_points']} points today.")
或者您可以使用 get() 方法,如下所示:
for dictionary in points:
print(f"Hi {dictionary.get('callsign')} you recieved {dictionary.get('daily_points')} points today.")
uj5u.com熱心網友回復:
你混合 dict 和 list ..
見下文
points = [{
"callsign": "A001",
"daily_points": 10,
"number": " 447774076621"
}, {
"callsign": "A002",
"daily_points": 5,
"number": " 447958708481"
}]
for point in points:
print(
f'Hi {point.get("callsign")} you received {point.get("daily_points")} points today.'
)
輸出
Hi A001 you received 10 points today.
Hi A002 you received 5 points today.
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/312942.html
