简体   繁体   English

Python 在循环中更新字典中的键和值

[英]Python updating keys and values in dictionary in loop

I'm new to programming, so I apologize in advance for any crappy code.我是编程新手,所以我提前为任何糟糕的代码道歉。 This example is a simplified version of my problem.这个例子是我的问题的简化版本。 I am obtaining data from two separate files, indicated here as lists (eg, filea and fileb).我正在从两个单独的文件中获取数据,此处表示为列表(例如,filea 和 fileb)。 What I would like to do is create a single dictionary (data_dict) for which the key is an id number;我想做的是创建一个字典(data_dict),其键是一个 id 号; here as the first element in a list (eg, 100).这里作为列表中的第一个元素(例如,100)。 The value would be a list which gets appended when updated.该值将是一个在更新时附加的列表。 In the first loop (filea), the id is appended to the value_list and then a data value is appended (for this example a9999) and then added to the dictionary for that key (id).在第一个循环 (filea) 中,将 id 附加到 value_list,然后附加数据值(对于此示例为 a9999),然后添加到该键 (id) 的字典中。

The problem that I'm having is trying to get the second loop (fileb) to append correctly.我遇到的问题是试图让第二个循环 (fileb) 正确附加。 The final dictionary is just the result from the second loop (fileb) as can been seen by the b9999.如 b9999 所示,最终字典只是第二个循环 (fileb) 的结果。 I'm clearly doing something wrong with extracting the value of the key from the first loop so that I can add the second data point in the second loop.我显然在从第一个循环中提取键的值时做错了,以便我可以在第二个循环中添加第二个数据点。 The final dictionary I am trying to achieve is {100: [100, 'a9999', 'b9999'] , 101: [100, 'a9999', 'b9999']} without the id begin appended to each list twice (eg, [100, 'a9999', 100, 'b9999'])我试图实现的最终字典是 {100: [100, 'a9999', 'b9999'] , 101: [100, 'a9999', 'b9999']} 没有将 id begin 附加到每个列表两次(例如, [100, 'a9999', 100, 'b9999'])

filea = [[100,1],[101,1]]
fileb = [[100,2],[101,2]]

def my_func():
    data_dict = {} # a dictionary to hold all data
    for file in [[filea],[fileb]]: 
        name = extra_func(file) #this is only added for the simplified example
        for lists in file: 
            for digit_list in lists:
                value_list = [] # the list that will be the value of each key in data_dict
                id = digit_list[0] #the first item in the list will be the id number
                digit_list.pop(0) #then drop the id number from the list
                data_dict[id] = id #create a key based on the id number 
                #print [data_dict.get(id)] # get the value for that key
                value_list.append(id) #append the id to the value_list
                data = 9999 #this is a placeholder for the example
                value_list.append(name + str(data)) #append the data with name (a or b) for readability
                data_dict[id] = value_list #add the value the key (id)
                #print "data_dict for ", id, data_dict,"\n"
            print "data_dict for all ids in file",name, "\n", data_dict,"\n"
    return data_dict

def extra_func(file):
    if file == [filea]: #this is only added for the simplified example
        name = 'a'
    if file == [fileb]:
        name = 'b'
    return name

data_dict = my_func()
print "final dictionary", data_dict

The first line in your inner loop is the start of the problem: you always begin with a fresh list.内部循环的第一行是问题的开始:你总是从一个新的列表开始。 Instead, use the dict.get method to get what you want for the starting list.相反,使用 dict.get 方法来获取您想要的起始列表。 Then just add the new data.然后只需添加新数据。

    for lists in file:
        for digit_list in lists:
            # Get the existing list for this ID.
            # If none, start a new one.
            id = digit_list[0] #the first item in the list will be the id number
            value_list = data_dict.get(id, [id])

            digit_list.pop(0) #then drop the id number from the list
            data_dict[id] = id #create a key based on the id number 
            #print [data_dict.get(id)] # get the value for that key
            data = 9999 #this is a placeholder for the example
            value_list.append(name + str(data)) #append the data with name (a or b) for readability
            data_dict[id] = value_list #add the value the key (id)
            #print "data_dict for ", id, data_dict,"\n"
        print "data_dict for all ids in file",name, "\n", data_dict,"\n"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM