为什么字典值在此循环结束时被覆盖?

时间:2014-01-25 03:35:12

标签: python csv dictionary

我有一个名为stop的公共交通站点词典。我想复制那些转移(有多行),以便在每个附加行的停靠点中有一个重复停止。我最初将这些重复项存储在名为duplicates的字典中。但是,在为每个重复停靠点指定相应行的名称后,它们都会被原始停靠点的原始行列表中的最后一行覆盖。所以我最终得到了一堆重复的停止,所有停止都是相同的行,而不是每行一个停止。什么是压倒这些价值观?文件l_stops.csv位于Dropboxbpaste

import csv
import random

def stop_coords():
    with open('l_stops.csv', 'rb') as csvfile:
        stop_reader = csv.reader(csvfile, delimiter=',', quotechar='"')
        stops = {}
        for row in stop_reader:
            map_id = row[5]
            lines = set()
            if row[7] == 'true':
                lines.add('Red')
            if row[8] == 'true':
                lines.add('Blue')
            if row[9] == 'true':
                lines.add('Green')
            if row[10] == 'true':
                lines.add('Brown')
            if row[11] == 'true':
                lines.add('Purple')
            if row[13] == 'true':
                lines.add('Yellow')
            if row[14] == 'true':
                lines.add('Pink')
            if row[15] == 'true':
                lines.add('Orange')
            if map_id not in stops:
                stop_name = row[2].partition('(')[0].rstrip(' ')
                lat = float(row[16].lstrip('"(').rpartition(',')[0])
                lng = float(row[16].lstrip('"(').rpartition(',')[2].strip(' )"'))
                stop = {}
                stop['name'] = stop_name
                stop['lat'] = lat
                stop['lng'] = lng
                stop['x'] = lng
                stop['y'] = lat
                stop['lines'] = lines
                stops[map_id] = stop
                stop['duplicateStops'] = []
            elif stops[map_id]['lines'] != lines:
                stops[map_id]['lines'] = stops[map_id]['lines'].union(lines)
        for item in stops:
            stops[item]['lines'] = list(stops[item]['lines'])

        # Add duplicate stops for stops that are transfers (shared by multiple lines)
        duplicates = {} # the dictionary that will hold the duplicates and be added to the stops dictionary after all duplicate stops have been processed
        for item in stops:
            num_lines = len(stops[item]['lines'])
            if num_lines > 1: # if a stop has more than one line
                original_lines = stops[item]['lines']
                stops[item]['lines'] = original_lines[0]
                equivalent_map_ids = [item] # Make a list of different map_ids that represent the same stop (but on different lines). The first map_id in the list will be the "original" one.
                for i in range(num_lines - 1): # for each line after the first one
                    # Create a new map_id and make sure it doesn't conflict with an existing map_id
                    while True:
                        new_map_id = str(random.randint(10000, 99999))
                        if new_map_id not in stops and new_map_id not in duplicates:
                            break
                    duplicates[new_map_id] = stops[item] # duplicate the stop
                    equivalent_map_ids.append(new_map_id) # add the new map_id to the list of equivalent map_ids
                # Set the duplicateStops value of everyone in equivalent_map_ids's to the other stops' map_ids
                # The first map_id in equivalent_map_ids is the original one that's in the stops dictionary, so set its duplicateStops value to the rest of the list
                stops[item]['duplicateStops'] = equivalent_map_ids[1:]

                # For the rest of the map_ids in equivalent_map_ids
                j = 1
                for duplicate_stop in stops[item]['duplicateStops']:
                    duplicates[duplicate_stop]['lines'] = original_lines[j]
                    duplicates[duplicate_stop]['duplicateStops'] = equivalent_map_ids[:j] + equivalent_map_ids[(j + 1):]  # this line also changes stops[item]['duplicateStops'], not sure how
                    j+= 1
                # somehow by this point all duplicates have the same line (the last line in the original 'lines' list)
                for stop in stops[item]['duplicateStops']:
                    print duplicates[stop]['name']
                    print duplicates[stop]['lines']

        for item in duplicates:
            print item
            print duplicates[item]['name']
            print duplicates[item]['lines']
        stops.update(duplicates)
        stops['none'] = {'name' : 'none', 'lat' : 0, 'lng' : 0, 'x' : 0, 'y' : 0, 'lines' : ['none']}

在调试时,我发现重新分配重复项[duplicate_stop] ['duplicateStops']也会重新分配停止[item] ['duplicateStops']。怎么可能?重复和停止是两个单独的词典。

1 个答案:

答案 0 :(得分:2)

然后duplicates[duplicate_stop]stops[item] 命名 相同的对象 - 并改变 对象,嗯,更改 对象。对象 不会自动复制/克隆/复制到赋值或用作函数参数时。

有问题的一行很可能是

duplicates[new_map_id] = stops[item] # duplicate the stop

..且评论错误,因为发生 no 重复。


问题Understanding dict.copy() - shallow or deep?可能有用;至少它显示了如何制作真正的副本。