【问题标题】:key error when building dict with arrays使用数组构建 dict 时出现关键错误
【发布时间】:2017-01-31 22:27:02
【问题描述】:

我正在尝试构建两个字典,一个具有偶数街道值,一个具有奇数街道值。每条街道都有一个 Ref_ID,我希望每个 dict 将这些值用作键,并将它们对应的序列号用作值。

我看到以前的帖子用数组作为值来制作字典: append multiple values for one key in Python dictionary

我尝试在我的代码中执行此操作,但我认为偶数和奇数的条件以及使用 arcpy.SearchCursor 会增加代码的复杂性:

import arcpy

#service location layer
fc = r"J:\Workspace\FAN3 sequencing3\gisdb\layers.gdb\Rts_239_241_314_GoLive"

# create variables

f1 = "Route"
f2 = "Ref_ID"
f3 = "Sequence"
f4 = "Street_Number"

# create containers

rSet = set()
eLinks = dict()
oLinks = dict()

# make a route list

with arcpy.da.SearchCursor(fc, f1) as cursor:
    for row in cursor:
        rSet.add(row[0])
    del row

# list of even side street sequences
eItems = []
eCheckStreet = []

# list of odd side street sequences
oItems = []
oCheckStreet = []

# make two dicts, one with links as keys holding sequence values for the even side of the street
# the other for the odd side of the street

for route in rSet:
    with arcpy.da.SearchCursor(fc, [f2,f3,f4]) as cursor:
        for row in cursor:
            if row[2] != '0' and int(row[2]) % 2 == 0:
                if row[0] in eLinks:
                    eLinks[str(row[0])].append(row[1])
                else:
                    eLinks[str(row[0])] = [row[0]]
            elif row[2] != '0' and int(row[2]) % 2 != 0:
                if row[0] in eLinks:
                    oLinks[str(row[0])].append(row[1])
                else:
                    oLinks[str(row[0])] = [row[0]]
        del row

print eLinks, oLinks

输出是 Ref_ID 作为键和值。我尝试更改索引只是为了看看我是否会有所不同,但它仍然是一样的。我也尝试在 eLinks 中转换 if str(row[0]) 但无济于事。

【问题讨论】:

  • 我不知道这是否能解决问题,但在elif 之后,您的if 正在eLinks 中寻找row[0],而它可能应该在oLinks 中寻找。这将始终返回False。您可能想要同时创建eLinksoLinks defaultdict(list),这样您就不必手动初始化(即您可以跳过嵌套的ifs)。
  • 感谢 Mikk 指出 oLinks 错字。我改变了它并使用了集合,但得到了相同的结果。然后我摆脱了嵌套的 if ,它就像一个魅力。继续发布答案,我会检查它。谢谢!
  • 实际上,我认为你应该回答你自己的问题,我不是 100% 确定你做了什么 :-)
  • 按照您的建议,我使用了 collections.defaultdict(list) 并摆脱了嵌套的 if。

标签: python dictionary multidimensional-array arcpy


【解决方案1】:

问题可能在于嵌套的if 以及这些条件如何相互作用。您不必承担对字典进行标准键检查的责任:有一个内置的数据结构可以做到这一点:collections.defaultdict:https://docs.python.org/2/library/collections.html#collections.defaultdict

import arcpy
from collections import defaultdict

#service location layer
fc = r"J:\Workspace\FAN3 sequencing3\gisdb\layers.gdb\Rts_239_241_314_GoLive"

# create variables

f1 = "Route"
f2 = "Ref_ID"
f3 = "Sequence"
f4 = "Street_Number"

# create containers

rSet = set()
eLinks = defaultdict(list)
oLinks = defaultdict(list)

# make a route list

with arcpy.da.SearchCursor(fc, f1) as cursor:
    for row in cursor:
        rSet.add(row[0])
    del row


# make two dicts, one with links as keys holding sequence values for the even side of the street
# the other for the odd side of the street

for route in rSet:
    with arcpy.da.SearchCursor(fc, [f2,f3,f4]) as cursor:
        for row in cursor:
            if row[2] != '0' and int(row[2]) % 2 == 0:
                eLinks[str(row[0])].append(row[1])
            elif row[2] != '0' and int(row[2]) % 2 != 0:
                oLinks[str(row[0])].append(row[1])
        del row
print eLinks, oLinks

【讨论】:

    猜你喜欢
    • 2016-01-26
    • 1970-01-01
    • 2019-02-07
    • 1970-01-01
    • 2018-05-04
    • 2018-01-22
    • 1970-01-01
    • 1970-01-01
    • 2022-06-24
    相关资源
    最近更新 更多