Retain all entries except for one key python

后端 未结 7 587
情书的邮戳
情书的邮戳 2020-12-10 10:16

I have a python dictionary. Just to give out context, I am trying to write my own simple cross validation unit.

So basically what I want is to get all the values exc

7条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-10 10:39

    Given a dictionary say

    d = {
         2: 2, 5: 16, 6: 5,
         7: 6, 11: 17, 12: 9,
         15: 18, 16: 1, 18: 16,
         19: 17, 20: 10
         }
    

    then the simple comprehension example would attain what you possibly desire

    [v for k,v in d.iteritems() if k not in (2,5)]
    

    This example lists all values not with keys {2,5}

    for example the O/P of the above comprehension is

    [5, 6, 1, 17, 9, 18, 1, 16, 17, 10]
    

提交回复
热议问题