Python:遍历嵌套字典时的冗余

2024-04-19 03:57:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个嵌套字典,我正试图通过循环,以便写入excel文件。你知道吗

这是启动和创建嵌套字典的代码

def tree(): return defaultdict(tree)
KMstruct = tree()
for system in sheet.columns[0]:
if system.value not in KMstruct:
    KMstruct[system.value]
    for row in range(1,sheet.get_highest_row()+1):
        if sheet['A'+str(row)].value == system.value and sheet['B'+str(row)].value not in KMstruct:
            KMstruct[system.value][sheet['B'+str(row)].value]
            if sheet['B'+str(row)].value == sheet['B'+str(row)].value and sheet['C'+str(row)].value not in KMstruct:
                KMstruct[system.value][sheet['B'+str(row)].value][sheet['C'+str(row)].value]
                if sheet['C'+str(row)].value == sheet['C'+str(row)].value and sheet['D'+str(row)].value not in KMstruct:
                    KMstruct[system.value][sheet['B'+str(row)].value][sheet['C'+str(row)].value][sheet['D'+str(row)].value]
                    KMstruct[system.value][sheet['B'+str(row)].value][sheet['C'+str(row)].value][sheet['D'+str(row)].value] = [sheet['E'+str(row)].value]

这是我循环的代码:

for key in KMstruct.keys():
r += 1
worksheet.write(r, col,     key)
for subkey in KMstruct[key]:
    if currsubkeyval != subkey:
        r += 1
        worksheet.write(r, col,     key)
    r +=1
    worksheet.write(r, col, key + '\\' + subkey)
    for item in KMstruct[key][subkey]:
        if curritemval != item:
            r +=1
            worksheet.write(r, col, key + '\\' + subkey)
        for subitem in KMstruct[key][subkey][item]:
            r += 1
            worksheet.write(r, col, key + '\\' + subkey + '\\' + item)
            worksheet.write(r, col + 1, subitem)
            curritemval = item
            for finalitem in KMstruct[key][subkey][item][subitem]:
                r += 1
                worksheet.write(r, col, key + '\\' + subkey + '\\' + item + '\\' + subitem)
                worksheet.write(r, col + 1, KMstruct[key][subkey][item][subitem])

忍受我的这个代码,因为我是一个noob,我知道这不是那么漂亮。不管怎样,我的问题是最后一个循环。我试图获取KMstruct[key][subkey][item][subitem]中的字符串值,但是循环变量lastitem遍历键的字符串值的每个字符(注意:键subitem包含字符串列表)。这意味着,如果我只有一个要写入的值,那么它的写入次数与字符串中的字符数相同。你知道吗

例如:value:apple将被写入新的excel行5次

我做错什么了?你知道吗

编辑:关于冗余的问题已经解决了,但是现在我需要了解在将我的最后一个项,即我的字符串列表,分配给子项键时是否做错了什么。你知道吗


Tags: keyinforvaluecolitemsystemwrite
2条回答

问题是在Python中,str也是iterable,例如:

>>> for s in 'hello':
...    print(s)

h
e
l
l
o

因此,您要么需要避免在值为str时进行迭代,要么将str包装在另一个iterable中(例如,a list),以便以相同的方式处理它。这是由于你是如何构建你的代码稍微困难。例如,以下内容:

for key in KMstruct.keys():
    for subkey in KMstruct[key]:

…可以写成:

for key, value in KMstruct.items():
    for subkey, subvalue in value.items():

…为每个循环提供,使应用测试成为可能。你知道吗

for key, val in KMstruct.items():
r += 1
worksheet.write(r, col,     key)

for subkey, subval in val.items():
    if currsubkeyval != subkey:
        r += 1
        worksheet.write(r, col,     key)
    r +=1
    worksheet.write(r, col, key + '\\' + subkey)

    for item, itemval in subval.items():
        if curritemval != item:
            r +=1
            worksheet.write(r, col, key + '\\' + subkey)

        for subitem, subitemval in itemval.items():
            r += 1
            worksheet.write(r, col, key + '\\' + subkey + '\\' + item)
            worksheet.write(r, col + 1, subitem)
            curritemval = item

            # I am assuming at this point subitemval is either a list or a str?
            if type(subitemval) == str:
                # If it is, wrap it as a list, so the next block works as expected
                subitemval = [subitemval] 

            for finalitem in subitemval:
                r += 1
                worksheet.write(r, col, key + '\\' + subkey + '\\' + item + '\\' + subitem)
                worksheet.write(r, col + 1, finalitem) # This should be finalitem, correct?

这里我们测试subitemval的类型,如果它是str,则将它包装在一个列表[str]中,这样下面的块就会按预期进行迭代。还有一个明显的错误,您没有在最后一行输出finalitem。在没有示例数据的情况下不可能进行测试,但在功能上应该是等效的。你知道吗

你眼前的问题是,你的例子的最后一行应该是:

                worksheet.write(r,col+1, finalitem)

顺便说一句,如果您偶尔创建一些临时变量,比如:

        subitemlist = KMstruct[key][subkey][item]
        for subitem in subitemlist:

相关问题 更多 >