Python不适合

Moc*_*ing 2 python for-loop

我有一个test声明:

test = {'test1': 1, 'test2': 2, 'test3': 3}
Run Code Online (Sandbox Code Playgroud)

我想复制一份test过滤掉可能存在或不存在的特定密钥的副本。

我尝试了以下方法:

test_copy = {k: test[k] for k not in ('test3', 'test4')}
Run Code Online (Sandbox Code Playgroud)

但是Python似乎不喜欢 for not in循环。有什么办法可以很好地做到这一点吗?

我不认为此问题与if语句List理解的重复, 因为我专门为字典搜索了几分钟以上。

ppp*_*ery 5

字典理解test_copy = {k: test[k] for k in test if k not in EXCLUDED_KEYS}将完成复制。