我有一个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循环。有什么办法可以很好地做到这一点吗?