可使用字典的popitem方法,循环删除字典中最后一个键值对。
d={'a':1,'b':2,'c':3} N=2 for i in range(len(d)-N): d.popitem() print(d) #运行: F:\2021\qa\ot2>t8 {'a': 1, 'b': 2}