python ---處理字典方法,將相同的key進行合並,value值用列表展示


test=[
{"url":"/order.html","code":"post"},
{"url":"/order.html","code":"get"},
{"url":"/order.html","code":"put"},
{"url":"/order.html","code":"delete"},
{"url":"/index.html","code":"post"},
{"url":"/index.html","code":"get"},
{"url":"/index.html","code":"put"},
{"url":"/login.html","code":"put"},
{"url":"/login.html","code":"get"},
{"url":"/login.html","code":"post"},
]
handler={}
for item in test:
if item['url'] in handler:
handler[item["url"]].append(item["code"])
else:
handler[item["url"]] = [item["code"]] #用列表方式接收

print(handler)

#{'/order.html': ['post', 'get', 'put', 'delete'], '/index.html': ['post', 'get', 'put'], '/login.html': ['put', 'get', 'post']}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM