python 如何对字典循环中删除某一个元素
#--encoding:utf-8 #===============如何对字典循环操作删除并某一个元素==================== d_demo = dict() d_demo["name"] = "jim" d_demo["age"] = 25 ''' # 这样直接循环字典,并对字典进行删除元素操作,会抛出 # RuntimeError: dictionary changed size during iteration for s_key in d_demo: if s_key=="name": d_demo.pop(s_key) ''' #这样才是正确的 for s_key in list(d_demo.keys()): if s_key == "name": d_demo.pop(s_key) print(d_demo) #{'age': 25}