将值作为列表添加到字典键
add value to dictionary key as list
是否可以将字典的键值视为列表?
如何向该列表附加新值?
#my dictionary
mydic = {}
#my key
username = "praveen"
#my value
user_accessKey = "tmp1"
user_accessKey2 = "tmp2"
如何使用我的键和值构建这样的字典?
print (mydic)
{"praveen":["tmp1","tmp2"]}
简单的字典赋值
mydic[username] = [user_accessKey, user_accessKey2]
附加值(感谢@wjandrea 的建议):
mydic.setdefault(username, []).append(new_value)
最简单的就是直接做:
mydic = {username: [user_accessKey, user_accessKy2]}
是否可以将字典的键值视为列表?
如何向该列表附加新值?
#my dictionary
mydic = {}
#my key
username = "praveen"
#my value
user_accessKey = "tmp1"
user_accessKey2 = "tmp2"
如何使用我的键和值构建这样的字典?
print (mydic)
{"praveen":["tmp1","tmp2"]}
简单的字典赋值
mydic[username] = [user_accessKey, user_accessKey2]
附加值(感谢@wjandrea 的建议):
mydic.setdefault(username, []).append(new_value)
最简单的就是直接做:
mydic = {username: [user_accessKey, user_accessKy2]}