我通过扫描文档进行阅读中每一行的列表。根据列表元素和列表字典键的值
我保存这一个叫
testList = []
当我名单完成填充此列表我想将其设置为一个字典的值,其中的关键是基于另一个列表的元素。 的想法是它应该是这个样子:
testList = ['InformationA', 'InformationB', 'Lastinfo']
patent_offices = ['European Office', 'Japan Office']
dict_offices[patent_offices[0]] = testList
或
dict_offices = {'European Office' : ['InformationA', 'InformationB', 'Lastinfo'],
'Japan Office' : ['Other list infoA', 'more infoB']}
我想稍后再输入dict_offices['European Office']
并得到打印的清单。
但是因为我在阅读文档时动态地收集这些数据,所以我删除并重新使用了testList
。我看到的是在它被清除之后,它在字典的链接中也被清除了。
如何创建字典以便保存,以便每次循环都可以重复使用testList?
这里是我的代码:
patent_offices = []
dict_offices = {}
office_index = 0
testList = []
# --- other conditional code not shown
if (not patent_match and start_recording):
if (not re.search(r'[=]+', bString)): #Ignore ====== string
printString = fontsString.encode('ascii', 'ignore')
testList.append(printString)
elif (not start_recording and patent_match):
dict_offices[patent_offices[office_index]] = testList
start_recording = True
office_index += 1
testList[:] = []
这本词典是正确更新和长相酷似我想让它,直到我叫
testList[:] = []
线。这本字典像testList
一样空白。我知道字典与此有关,但我不知道如何避免这种情况发生。
感谢您的帮助。我只是需要正确使用这些更新等。 – TWhite 2013-02-14 16:32:42