在for循环上将键添加到字典中,获取运行时错误

时间:2019-12-13 06:59:26

标签: python loops dictionary for-loop

我正在向具有值的字典添加新键,但是在运行代码时始终遇到运行时错误。我在同一迭代中有时会添加两个单独的键。我可以在添加一个键时同时运行它,但不能同时添加两个键。

用于将无效的项目添加到字典的代码:


    for data in list:
        for value in data:
            if value == 'timestamp':
                timestamp = data[value]
                minute_shred = timestamp[14:16]
                second_shred = timestamp[17:19]
                    #change to x,y
                if minute_shred[0] == '0' and second_shred[0] == '0':
                    minute_shred = timestamp[15:16]
                    second_shred = timestamp[17:19]
                    data['minute'] = int(minute_shred)
                    data['second'] = int(minute_shred)
                    print(data['second'], timestamp)
                    #no change to x,y
                elif minute_shred[0] != '0' and second_shred[0] != '0':
                    data['minute'] = int(minute_shred)
                    data['second'] = int(minute_shred)
                    print(data['second'], timestamp)
                    # change to x, not y
                elif minute_shred[0] == '0' and second_shred[0] != '0':
                    minute_shred = timestamp[15:16]
                    data['minute'] = int(minute_shred)
                    data['second'] = int(second_shred)
                    print(data['second'], timestamp)
                    #change to y, not x
                elif minute_shred[0] != '0' and second_shred[0] == '0':
                    second_shred = timestamp[18:19]
                    data['second'] = int(second_shred)
                    data['minute'] = int(minute_shred)
                    print(data['second'], timestamp)

仅在迭代中添加一个键时,此代码才有效。

    for data in list:
        for value in data:
            if value == 'timestamp':
                timestamp = data[value]
                minute_shred = timestamp[14:16]
                if minute_shred[0] == '0':
                    minute_shred = timestamp[15:16]
                    data['minute'] = int(minute_shred)
                elif minute_shred[0] != '0':
                    data['minute'] = int(minute_shred)

我还尝试了一个接一个地添加它们,但是没有用,第二个for循环带有添加数据的逻辑['second],因为它会产生运行时错误。我确实注意到,如果在添加second_shred变量时将data ['second']更改为data ['minute],则它可以正常工作。显然,我需要将它们添加为两个单独的键。

2 个答案:

答案 0 :(得分:2)

就像其他人提到的那样,在迭代时不要修改它。

如果您要迭代和修改数据,我强烈建议使用地图或列表理解。

示例:

def modify(dict_val):
    # Code for modification as in your question

list = [map(modify, data) for data in list]

如果您不想在以后的迭代中使用它,请确保将地图包装在list()中(如果是Python3)。

答案 1 :(得分:0)

使用Deepcopy工作。

    listcopy = copy.deepcopy(list)
    for item in listcopy:
        itemcopy = copy.deepcopy(item)
        for k in itemcopy:
            if k == 'timestamp':
                timestamp = item[k]
                second_shred = timestamp[17:19]
                if second_shred[0] == '0':
                    second_shred = timestamp[18:19]
                    item['second'] = int(minute_shred)
                elif second_shred[0] != '0':
                    item['second'] = int(minute_shred)