【问题标题】:converting dictionary of list to dictionary of required format将列表字典转换为所需格式的字典
【发布时间】:2022-01-03 15:01:05
【问题描述】:

我有 ip 数据,

{'bill_source': [['U', 10], ['B', 43],['D',4]],'bill_target': [['B', 43], ['S', 17], ['U', 10]]}

并希望将其转换为值、源键和目标键, 即

op={bill:[{value:"U",source:10,target:19},{value:"B",source:43,target:43},{value:"D",source:4,target:NULL},{value:"S",source:NULL,target:17}]}

我尝试了一些基本的 Python 知识,但想改进它。 下面是我的代码,

from yaml import safe_load

data={'bill_source': [['K',20],['U', 10], ['B', 43]],'bill_target': [['B', 43], ['S', 17], ['U', 10]]}
source=[]
target=[]
kpi=[]

for key in data.keys():
    if '_source' in key:
        value=key.replace('_source','')
        if value not in kpi:
            kpi.append(value)
    if '_target' in key:
        value=key.replace('_target','')
        if value not in kpi:
            kpi.append(value)
            
uniq_value_key_pair=[]        
a=[]
for i in data.keys():
    #print(data[i])
    for j in range(0,len(data[i])):
        value=data[i][j]
        if value not in uniq_value_key_pair:
            uniq_value_key_pair.append(value)

        
#print(all_uniq_values)
                 

l = uniq_value_key_pair
d = {}
for key, val in l:
    d.setdefault(key, []).append(val)
uniq_values=[]
for key in d.keys():
    if key not in uniq_values:
        uniq_values.append(key)

final={}
f1=[]
f2=[]
print('uniq:',uniq_values)
print('kpi:',kpi)
data1=''
s1=''
final_dict={}
for main_kpi in kpi:
    for key in data.keys():
        if main_kpi.lower() in key.lower() and 'source' in key.lower():
            s_D = {}
            for key, val in data[key]:
                s_D.setdefault(key, []).append(val)

            for ct in range(0,len(uniq_values)):
                value=uniq_values[ct]
                final['value']=value
                if value in s_D.keys():
                    sour=s_D[value]
                else:
                    sour='NULL'
                final['source']=str(sour).replace('[', '').replace(']', '')
                final['target']='NULL'
                s1+=str(final)+'$$'
    
    f1=s1.split('$$')
    f1=list(filter(None,f1))

    
    
    for key in data.keys():
        if main_kpi.lower() in key.lower() and 'target' in key.lower():
            s_D = {}
            for key, val in data[key]:
                s_D.setdefault(key, []).append(val)    
    
    for l in range(0,len(f1)):
        d_val=safe_load(f1[l])
        val=d_val['value']
        if val in s_D.keys():
            tar=str(s_D[val]).replace('[', '').replace(']', '')
        else:
            tar='NULL'
        d_val['target']=tar
        f2.append(d_val)

    final_dict[main_kpi]=f2


print(final_dict)

对于这项任务,是否有更优雅的 Pythonic 解决方案?

【问题讨论】:

  • 好的...现在您可以就您尝试使用的代码提出问题了。

标签: python list dictionary


【解决方案1】:

下面的代码应该做你想做的:

d={'bill_source': [['U', 10], ['B', 43],['D',4]],
   'bill_target': [['B', 43], ['S', 17], ['U', 10]]}
bs = dict(d['bill_source'])
bt = dict(d['bill_target'])
keys = set().union(bs.keys(), bt.keys())
op = {'bill': [{'value': i, 'source': bs.get(i), 'target': bt.get(i)} for i in keys]}

【讨论】:

    猜你喜欢
    • 2018-08-21
    • 1970-01-01
    • 2021-01-01
    • 2018-12-24
    • 1970-01-01
    • 1970-01-01
    • 2017-04-23
    • 2021-10-13
    相关资源
    最近更新 更多