2 回答
TA贡献1844条经验 获得超8个赞
您可以从您的字典中过滤每个列表:
bad_items = pcps_in_negative | pcps_at_zero
new_members = {k: [e for e in v if e not in bad_items] for k, v in new_members.items()}
print(new_members)
输出:
{'member1': ['PCP2'],
'member10': ['PCP2'],
'member13': ['PCP5'],
'member2': ['PCP1', 'PCP2'],
'member5': ['PCP5'],
'member6': ['PCP1', 'PCP5'],
'member7': ['PCP2'],
'member8': ['PCP5'],
'member9': ['PCP1', 'PCP5']}
TA贡献1785条经验 获得超4个赞
尝试这个 :
import copy
for k, v in new_members.items():
temp_list = copy.deepcopy(v)
temp_list = [x for x in temp_list if x not in pcps_at_zero]
temp_list = [x for x in temp_list if x not in pcps_in_negative]
new_members[k] = temp_list
最终的 new_members 字典变为:
{'member1': ['PCP2'],
'member10': ['PCP2'],
'member13': ['PCP5'],
'member2': ['PCP1', 'PCP2'],
'member5': ['PCP5'],
'member6': ['PCP1', 'PCP5'],
'member7': ['PCP2'],
'member8': ['PCP5'],
'member9': ['PCP1', 'PCP5']}
添加回答
举报