如何使用if语句的字典键并填写另一个字典?

问题描述 投票:0回答:2

我这里有一个带结构的字典

'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>

我需要在C下为所有集合的所有通道“1”,“2”,“3”和“4”存储信息,这些集合超过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个频道的集合被填充。

感谢任何帮助。

python loops dictionary key
2个回答
0
投票

我找到了。代替

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

我应该用过

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

现在它有效。

感谢您的所有努力。


0
投票

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

例如:

if key in idict:
    <rest of the code>
© www.soinside.com 2019 - 2024. All rights reserved.