为什么未从列表中删除特定值?

时间:2019-06-17 07:13:32

标签: python python-3.x

我正在尝试完成以下简短练习: 迭代给定列表,并检查字典中是否存在给定元素作为键值(如果未从列表中删除)。

我的解决方案似乎可以正常工作,并从列表中删除所有项目,这些项目在字典中不存在,而是一个值... 95旁边的值。怎么可能?

在进行调试时,在循环的迭代过程中,Python似乎跳过了该值(95):

rollNumber = [47, 64, 69, 37, 76, 83, 95, 96, 97]
sampleDict = {"Jhon": 47, "Emma": 69, "Kelly": 76, "Jason": 97}

for current_item in rollNumber:
    if not current_item in sampleDict.values():
        rollNumber.remove(current_item)

print(rollNumber)

实际结果:

[47, 69, 76, 95, 97]

预期结果:

[47, 69, 76, 97]

1 个答案:

答案 0 :(得分:0)

尝试

            panel.addDomHandler(new FocusHandler() {
                @Override
                public void onFocus(FocusEvent focusEvent) {
                    panel.addStyleName("FOOSTYLE");
                }
            }, FocusEvent.getType());


            panel.addDomHandler(new BlurHandler() {
                FlowPanel _panel = focusPanel;
                boolean init = false;
                @Override
                public void onBlur(BlurEvent blurEvent) {
                    console.log("Do Blur");
                    panel.removeStyleName("FOOSTYLE");
                }
            }, BlurEvent.getType());

结果: rollNumber = [47, 64, 69, 37, 76, 83, 95, 96, 97] sampleDict = {"Jhon": 47, "Emma": 69, "Kelly": 76, "Jason": 97} rollNumber = [i for i in rollNumber if i in sampleDict.values()] print(rollNumber)