【问题标题】:How can I use if statements for dictionary keys and fill another dictionary?如何将 if 语句用于字典键并填充另一个字典?
【发布时间】:2019-04-11 14:44:00
【问题描述】:

我这里有一本结构式的字典:

'set1':{ 'status': 1,
            'data': {'1': {'C': ['116.587',
                                 '52.674',
                                 '23.164',
                                 '8.5'],
                            <sth>:...},
                    {'2': {'C': ['11.507',
                                 '50.674',
                                 '23.004',
                                 '8.02'],
                            <sth>:...},
                    {'3': {'C': ['16.587',
                                 '2.674',
                                 '3.164',
                                 '0.5'],
                            <sth>:...} 
                    {'4': {'C': ['0.587',
                                 '1.674',
                                 '3.009',
                                 '0.55'],
                            <sth>:...} 

'set2':{ 'status': 1,
             'data': {'3': {'C': ['116.587',
                                 '52.674',
                                 '23.164',
                                 '8.5'],
                            <sth>:...}
#<goes like this>

我需要将所有通道“1”、“2”、“3”和“4”的信息存储在 C 下,用于所有超过 100k 的集合。但并非所有的设备都有 4 个通道,但只有“3”和“4”,或者例如只有“1”和“2”。我正在尝试用零填充不存在的频道。

我尝试使用以下语句:

if( idict[n]["data"][c] ):

我认为如果这是真的,例如idict["set1"]["data"]["1"] 为真,它应该填充通道 '1',否则它应该填充 '0.000'。

idict 是输入字典,odict 是输出字典:

for n in idict: #n is the set number
    try:
        if(idict[n]["status"]==1 and idict[n]["data"]):
           #some has status=0 or has no data key. I need to ignore those
            odict[n] = []
            for c in ('1','2','3','4'):
                if( idict[n]["data"][c] ): #THIS IS WHAT I USED FOR THIS ISSUE
                    odict[n].append({
                        c : [
                            str(idict[n]["data"][c]["C"][0]),
                            str(idict[n]["data"][c]["C"][1]),
                            str(idict[n]["data"][c]["C"][2]),
                            str(idict[n]["data"][c]["C"][3])
                        ]
                        #indicies after ["C"] are for the 4 non integer entries 
                    })
                else:
                    odict[n].append({
                        c : ['0.000','0.000','0.000','0.000']
                    })
    except KeyError:
        continue

输出应该是这样的

'set1':{ 
                   {'1': ['116.587','52.674','23.164','8.5']
                    {'2': ['11.507','50.674','23.004','8.02']
                    {'3': ['16.587','2.674','3.164','0.5']
                    {'4': ['0.587','1.674','3.009','0.55']
        }

'set2':{ 
                     '3': ['116.587','52.674,'23.164','8.5'],
                     '4': [<something similar>]
         }
#<goes like this>

但是对于缺少某些通道的集合,我得到了空字典,但是具有 4 个通道的集合已被填满。

【问题讨论】:

  • 首先,使用这么大的try catch块不会显示错误。该错误实际上与您提到的某些集合缺少某些通道的原因相同。而不是修复channelID。将结果附加到该集合可用的键中。 :) 我会尝试发布代码。
  • 在添加此行 if( idict[n]["data"][c] ): 之前,它正在做同样的事情。它用 4 个通道的集合来填充 odict。但是,它甚至不会去这个 if 的 else 。不完整的集合被省略了,我不想这样做。

标签: python loops dictionary key


【解决方案1】:

我找到了。而不是

if( idict[n]["data"][c] ):

我应该用过

if c in idict[str(n)]["data"]:

现在可以了。

感谢所有的努力。

【讨论】:

    【解决方案2】:

    尝试使用 python 'in' 关键字来检查该键是否存在于字典中

    例如:

    if key in idict:
        <rest of the code>
    

    【讨论】:

    • 是的,它存在。不过,我在所有 unicode 字符的前面都得到了u。但我认为这不是问题,因为我可以毫无问题地填充输出字典和u
    猜你喜欢
    • 2018-03-27
    • 2022-12-24
    • 1970-01-01
    • 1970-01-01
    • 2022-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多