dict.setdefault(key,[]).append(value)
# coding:utf-8from loguru import logger as logs class demo: def run(self): new_dict = {} # 方法一 l = [('name', "zhangsan"), ('name', "lisi"), ('age', 13), ('age', 14), ('phone', "13211122234")] for k_v in l: k, v = k_v new_dict.setdefault(k, []).append(v) logs.debug(new_dict) # 方法二 name = ["zhangsan", "lisi", "wangwu"] addr = ["zhangsan1", "lisi1", "wangwu1"] news_dict = {} for i in name: news_dict.setdefault("name", []).append(i) for i in addr: news_dict.setdefault("addr", []).append(i) logs.debug(news_dict) if __name__ == "__main__": '''固定参数''' demo().run()
执行结果
标签:__,name,Python,value,dict,news,append,setdefault From: https://www.cnblogs.com/phoenixy/p/16911627.html