Thi*_*low 2 python dictionary list python-2.x python-2.7
我想将值附加到字典中的键,其中值实际上是列表.可能有一个列表,或者可能有多个列表.
我正在尝试这样做的当前方式,我尝试添加的新列表只是覆盖了最后一个列表.据我所知,我不能将列表附加到这样的字典:
my_dict.append(my_list))
Run Code Online (Sandbox Code Playgroud)
当前代码互相覆盖:
urls[domain] = [uri, datecreated]
Run Code Online (Sandbox Code Playgroud)
这可能吗?如果是这样,怎么样?
示例结果如下所示
print all keys and values in urls{}:
google.com : ['/helloworld', 2010-04-02], ['/apage/anotherpage', 2015-09-12]
microsoft.com : ['/awebpage', 2009-02-01], ['/bananas', 2015-12-24], ['/anothergreaturi', 205-12-10]
Run Code Online (Sandbox Code Playgroud)
您可以将每个词典条目都列为一个列表,以便附加到该列表.
>>> a = Linter("ahahah")
>>> a.write_file()
>>> a = dict()
>>> a["first"] = []
>>> a["first"].append([1,2,3])
>>> a["first"].append([4,5,6])
>>> a["second"] = []
>>> a["second"].append([7,8,9])
>>> a
{
'second':[
[7, 8, 9]
],
'first': [
[1, 2, 3],
[4, 5, 6]
]
}
Run Code Online (Sandbox Code Playgroud)
但老实说,在某些时候只考虑使用类?