python:循环遍历字典,每个键有多个值

时间:2018-07-02 08:11:24

标签: python dictionary

我有以下格言:

{'NodeId_Name': 'lvgwatchit01t', 'LocationId_Name': 'loc_gru,loc_virtual_vie', 'Name': 'aixbuildhost'}

如图所示,“ LocationId_Name”键具有两个用逗号分隔的值。我想做的是,遍历“ LocationId_Name”键并创建两个对应的 指示我可以移交给api调用。 输出应该是这样的。

{'NodeId_Name': 'lvgwatchit01t', 'LocationId_Name': 'loc_gru', 'Name': 'aixbuildhost'}
{'NodeId_Name': 'lvgwatchit01t', 'LocationId_Name': 'loc_virtual_vie', 'Name': 'aixbuildhost'}

我尝试了以下操作,但这只会打印出垃圾...

d_list = []
keys = ['Name', 'LocationId_Name', 'NodeId_Name']
add_data = {x:data_container[x] for x in keys}
print(add_data)
for key, values in add_data.iteritems():
    for value in values:
        d_list.append([key, value])
print(d_list)

[['NodeId_Name', 'l'], ['NodeId_Name', 'v'], ['NodeId_Name', 'g'], ['NodeId_Name', 'w'], ['NodeId_Name', 'a'], ['NodeId_Name', 't'], ['NodeId_Name', 'c'], ['NodeId_Name', 'h'], ['NodeId_Name', 'i'], ['NodeId_Name', 't'], ['NodeId_Name', '0'], ['NodeId_Name', '1'], ['NodeId_Name', 't'], ['LocationId_Name', 'l'], ['LocationId_Name', 'o'], ['LocationId_Name', 'c'], ['LocationId_Name', '_'], ['LocationId_Name', 'g'], ['LocationId_Name', 'r'], ['LocationId_Name', 'u'], ['LocationId_Name', ','], ['LocationId_Name', 'l'], ['LocationId_Name', 'o'], ['LocationId_Name', 'c'], ['LocationId_Name', '_'], ['LocationId_Name', 'v'], ['LocationId_Name', 'i'], ['LocationId_Name', 'r'], ['LocationId_Name', 't'], ['LocationId_Name', 'u'], ['LocationId_Name', 'a'], ['LocationId_Name', 'l'], ['LocationId_Name', '_'], ['LocationId_Name', 'v'], ['LocationId_Name', 'i'], ['LocationId_Name', 'e'], ['Name', 'a'], ['Name', 'i'], ['Name', 'x'], ['Name', 'b'], ['Name', 'u'], ['Name', 'i'], ['Name', 'l'], ['Name', 'd'], ['Name', 'h'], ['Name', 'o'], ['Name', 's'], ['Name', 't']]

背景是,该哑API无法在一次调用中处理两个值。

7 个答案:

答案 0 :(得分:2)

真的关闭!

此行:

for value in values:

...当values是一个字符串(确实是)时,它将遍历字符串中的字符,这就是为什么要获得输出的原因。

如果要将其更改为以下内容:

for value in values.split(','):

...您会得到想要的东西。

在这里,我们只在values字符串上调用.split()方法,其结果(在您的情况下为2元素列表)将被迭代

答案 1 :(得分:2)

假设您使用的是Python3.5或更高版本:

In [1]: d = {'NodeId_Name': 'lvgwatchit01t', 'LocationId_Name': 'loc_gru,loc_virtual_vie', 'Name': 'aixbuildhost'}
In [2]: k = 'LocationId_Name'
In [3]: vals = d[k].split(',')
In [4]: 
In [5]: dicts = [{**d, k:v} for v in vals]
In [6]: dicts
Out[6]: 
[{'LocationId_Name': 'loc_gru',
  'Name': 'aixbuildhost',
  'NodeId_Name': 'lvgwatchit01t'},
 {'LocationId_Name': 'loc_virtual_vie',
  'Name': 'aixbuildhost',
  'NodeId_Name': 'lvgwatchit01t'}]

答案 2 :(得分:1)

这是一个详细的分步解决方案:

my_dict = {'NodeId_Name': 'lvgwatchit01t', 'LocationId_Name': 'loc_gru,loc_virtual_vie', 'Name': 'aixbuildhost'}
# pop removes the item from the dict, split separates the string on the commas
locations = my_dict.pop('LocationId_Name').split(',')
print(locations)
# ['loc_gru', 'loc_virtual_vie']
output_dicts = []
for loc in locations:
    new_dict = {'LocationId_Name': loc}
    new_dict.update(my_dict)
    output_dicts.append(new_dict)
print(output_dicts)
# [{'LocationId_Name': 'loc_gru', 'NodeId_Name': 'lvgwatchit01t', 'Name': 'aixbuildhost'}, {'LocationId_Name': 'loc_virtual_vie', 'NodeId_Name': 'lvgwatchit01t', 'Name': 'aixbuildhost'}]

答案 3 :(得分:0)

使用简单的迭代和dict

例如:

d = {'NodeId_Name': 'lvgwatchit01t', 'LocationId_Name': 'loc_gru,loc_virtual_vie', 'Name': 'aixbuildhost'}
res = []
for i in d["LocationId_Name"].split(","):
    newVal = dict((j, i) if j == "LocationId_Name" else (j, d[j]) for j in d.keys()) 
    res.append(newVal)
print(res)

或单线

print( [dict((j, i) if j == "LocationId_Name" else (j, d[j]) for j in d.keys()) for i in d["LocationId_Name"].split(",")] )

输出:

[{'NodeId_Name': 'lvgwatchit01t', 'LocationId_Name': 'loc_gru', 'Name': 'aixbuildhost'}, {'NodeId_Name': 'lvgwatchit01t', 'LocationId_Name': 'loc_virtual_vie', 'Name': 'aixbuildhost'}]

答案 4 :(得分:0)

如果您使用的是python 3.5或更高版本

d = {'NodeId_Name': 'lvgwatchit01t', 'LocationId_Name': 'loc_gru,loc_virtual_vie', 'Name': 'aixbuildhost'}
d2 = [{**d, k:sv} for k,v in d.items() for sv in v.split(',') if ',' in v]
pprint(d2)

输出

[{'LocationId_Name': 'loc_gru',
  'Name': 'aixbuildhost',
  'NodeId_Name': 'lvgwatchit01t'},
 {'LocationId_Name': 'loc_virtual_vie',
  'Name': 'aixbuildhost',
  'NodeId_Name': 'lvgwatchit01t'}]

答案 5 :(得分:0)

{'NodeId_Name': 'lvgwatchit01t', 'LocationId_Name': 'loc_gru,loc_virtual_vie', 'Name': 'aixbuildhost'}

代码

for i in range(len(a["LocationId_Name"].split(","))):
    b = a.copy()
    b["LocationId_Name"] = b["LocationId_Name"].split(",")[i]
    print(b)

结果

{'NodeId_Name': 'lvgwatchit01t', 'LocationId_Name': 'loc_gru', 'Name': 'aixbuildhost'}
{'NodeId_Name': 'lvgwatchit01t', 'LocationId_Name': 'loc_virtual_vie', 'Name': 'aixbuildhost'}

答案 6 :(得分:-1)

在迭代之前使用values.split(','),它将变成一个列表。