我想将值附加到实际上是列表的字典的键中。可能有一个列表,也可能有多个列表。
我正在尝试的当前方式,我尝试添加的新列表只会覆盖最后一个列表。据我所知,我无法像这样向字典添加列表:
my_dict.append(my_list))
相互覆盖的当前代码:
urls[domain] = [uri, datecreated]
这可能吗?如果是这样,怎么办?
示例结果如下所示
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]
您可以使每个词典条目都有一个列表,因此您可以追加到该列表。
>>> 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]
]
}
但说实话,在某个时候只考虑使用类?
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句