【发布时间】:2017-03-24 02:36:05
【问题描述】:
为了澄清,我正在读取文件并将每一行发送到函数(1),其中相关元素被放入列表中。然后将该列表发送到另一个函数 (2) 并添加到字典中,该列表的一个元素作为键,其他元素放入另一个列表中,作为值。所以,基本上是 {key:(value(,value))。
问题是,每当我将列表从 (1) 发送到 (2) 时,新创建的字典都会被覆盖。我是 Python 新手,但我很确定我可以将多个键和值添加到一个字典中,对吗?那么,有没有办法在每次调用(2)时保存字典的元素?所以,如果它被调用一次,它在字典中有标记(a)。再次调用它时,它仍然有令牌(a),现在添加了令牌(b),依此类推。
如果您需要代码,我可以包含它。
MCVE:
def file_stuff(file name):
#opens and reads file using with x open as thing....then
for line in thing:
function1(line)
def function1(line):
#Breaks down line using regex; variable name var for this example
#list the relevant components of the line will go into
element = list()
for x in var:
list.append(x)
function2(element)
def function2(element):
#Another list is made along with a dictionary
webster = dict()
values = list()
for x in range(len(element)):
#inserts into dictionary.....the problem being, however, that I need this dictionary to "remember" what was already stored inside
【问题讨论】:
-
尝试创建mcve 来解释您的问题并将其发布在此处。帮助我们帮助您...
-
我提供的伪代码够不够?
标签: python list function python-3.x dictionary