简体   繁体   English

遍历 python 字典并在两个列表中附加关键组件

[英]Iterate through python dictionary and appending key component in two lists

I have the following dictionary, tempDict我有以下字典tempDict

{('M1', 'P1'): 6.0,
 ('M1', 'P2'): 10.0,
 ('M1', 'P3'): 4.0,
 ('M2', 'P1'): 7.0,
 ('M2', 'P2'): 9.0,
 ('M2', 'P3'): 5.0}

I am doing some operations over this dictionary and taking the some key components to a list.我正在对这本字典进行一些操作,并将一些关键组件列在一个列表中。 Say I have l1 = [], l2 = [] .假设我有l1 = [], l2 = []

Suppose minimum value is 4 the I find that jobsR as ('M1', 'P3') .I would like to remove the all the keys where 'P3' appears from the tempDict.假设最小值为 4,我发现jobsR as ('M1', 'P3') 。我想从 tempDict 中删除出现 'P3' 的所有键。

I will be finding the minimum value from this dictionary iteratively and will drop corresponding keys.我将迭代地从该字典中找到最小值,并将删除相应的键。 As the keys are ordered pairs, if the departed key element has 1st component M1 then I shall take the 2nd component in list l1 else in l2 .由于键是有序对,如果离开的键元素具有第一个组件M1 ,那么我将在列表l1中获取第二个组件,否则在l2中。 I shall continue until the dictionary becomes empty.我将继续,直到字典变空。 My code is,我的代码是,

while bool(tempDict):
    try:
        l1 = []
        l2 = []
        valMin = min(tempDict.values())
        jobsR = [key for key in tempDict if tempDict[key] == valMin]
        for (x, y) in jobsR:
            if x == 'M1':
                l1.append(y)
            else:
                l2.append(y)
        remove_list = []
        for key, value in tempDict.items():
            if jobsR[0][1] in key[1]:
                remove_list.append(key)
        for item in remove_list:
            tempDict.pop(item)
    except KeyError:
        print('The dictionary has no item now...')
        break

Expected output:预期 output:

l1 = [P3, P1] and l2 = [P2]

Code_Updated代码_更新

l1 = []
l2 = []
while bool(tempDict):
    valMin = min(tempDict.values())
    jobsR = [key for key in tempDict if tempDict[key] == valMin]
    remove_list = []
    for key, value in tempDict.items():
        if jobsR[0][1] in key[1]:
            remove_list.append(key)
    for item in remove_list:
        tempDict.pop(item)
    for x in jobsR:
        #print(x[0])
        if x[0] == 'M1':
            l1.append(item[1])
        else:
            l2.append(item[1])

lists l1 and l2 are set into [ ] in every cycle of the while loop.列表 l1 和 l2 在 while 循环的每个循环中都设置为 [ ]。 Declare l1 and l2 outside of the while loop to get the expected output.在 while 循环之外声明 l1 和 l2 以获得预期的 output。

You need to take l1 and l2 out of the while loop and pop keys as you go:您需要将l1l2从 while 循环和弹出键中取出 go:

l1 = []
l2 = []
while tempDict:
    min_val = min(tempDict.values())
    jobsR = [k for k,v in tempDict.items() if v==min_val][0]
    if jobsR[0] == 'M1':
        l1.append(jobsR[1])
    else:
        l2.append(jobsR[1])
    for k in tempDict:
        if k[1]==jobsR[1]]:
            tempDict.pop(k)

Output: Output:

# l1 = ['P3', 'P1']
# l2 = ['P2']

Note: This assumes each minimum value has a unique key.注意:这假设每个最小值都有一个唯一的键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM