![](/img/trans.png)
[英]Python: Adding dictionary value pair only if they are Unique. If they are already present, I need to print the dictionay values
[英]Adding key:value items to a dictionay in python with a for
嗨,我有這個功能:
def company_runtime(company_name):
for well in all_wells:
well_runtime = {}
if well["groupName"].lower() == company_name.lower():
well_runtime.update({well["name"]: well["efficiency"]})
return well_runtime
如果我輸入compay名稱作為參數,我想從循環中獲取具有“名稱”和“效率”的字典。
我只得到滿足該條件的all_wells列表的最后一個元素。
我做錯了什么?
提前致謝
每次您執行循環操作時,都會將Dictionary重置為{}
或為空,這樣它將清除它,直到最后一次退出。 將well_runtime = {}
移動到循環之外。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.