Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 基于列表元素和列表值的字典键_Python_List_Dictionary_Python 2.7 - Fatal编程技术网

Python 基于列表元素和列表值的字典键

Python 基于列表元素和列表值的字典键,python,list,dictionary,python-2.7,Python,List,Dictionary,Python 2.7,我正在浏览一份文件,列出每一行读到的内容 我正在把它保存到一个名为 testList = [] 填充完此列表后,我希望将其设置为字典中的值,该字典的键基于另一个列表的元素。 想法是它应该看起来像这样: testList = ['InformationA', 'InformationB', 'Lastinfo'] patent_offices = ['European Office', 'Japan Office'] dict_offices[patent_offices[0]] = testL

我正在浏览一份文件,列出每一行读到的内容

我正在把它保存到一个名为

testList = []
填充完此列表后,我希望将其设置为字典中的值,该字典的键基于另一个列表的元素。 想法是它应该看起来像这样:

testList = ['InformationA', 'InformationB', 'Lastinfo']
patent_offices = ['European Office', 'Japan Office']
dict_offices[patent_offices[0]] = testList

我想稍后键入
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
一样变成空白。我知道字典与此相关,但我不知道如何避免这种情况发生。

列表是可变的;对同一列表的多个引用将看到您对其所做的所有更改<代码>测试列表[:]=[]的意思是:用空列表替换此列表中的每个索引。由于在不同的位置(包括字典值中)引用相同的列表,因此您可以看到到处都反映出的更改

相反,只需将
testList
指向一个新的空列表即可:

testList = []
仅当您想要清除列表的内容时,才应该使用您使用的空片分配语法,而不是当您仅仅想要创建一个新的空列表时

>>> foo = []
>>> bar = foo
>>> foo.append(1)
>>> bar
[1]
>>> foo is bar
True
>>> foo[:] = []
>>> bar
[]
>>> foo = ['new', 'list']
>>> bar
[]
>>> foo is bar
False

你必须做一个深度复制

例如:

l = [1,2,3]
b = l
del l[:]
print(b)
 ---> []

l = [1,2,3]
b = copy.deepcopy(l)
l = []
print (b)
  ---> [1,2,3]

谢谢你的帮助。我只是需要这些关于正确使用的更新,等等。
dict_offices[patent_offices[office_index]] = copy.deepcopy(testList)
l = [1,2,3]
b = l
del l[:]
print(b)
 ---> []

l = [1,2,3]
b = copy.deepcopy(l)
l = []
print (b)
  ---> [1,2,3]