【发布时间】:2016-11-09 03:44:21
【问题描述】:
我正在迭代字典中的旧键。我已经在函数外部测试了 for 循环并获得了预期的结果,但是当我将它放入函数时,它会返回旧键。
一直在讨论论坛,但一无所获。
edu_dict = {
"A.S":"Attended Vocational/Technical",
"AAS":"Attended Vocational/Technical",...etc}
def clean_edu(edu_dict):
for i in list(edu_dict):
key = i # accesing the key from the edu_dic
lower_case = key.lower() # changing the key to lower case
char_change = "".maketrans(chars_in, chars_out)
clean = lower_case.translate(char_change)
y = re.sub(r'\s\s{1,}','',clean)
edu_dict[y] = edu_dict.pop(i)
return edu_dict
clean_edu(edu_dict)
print(edu_dict)
这似乎是一个简单的解决方案,但我很难过。
【问题讨论】:
-
这似乎是可行的解决方案:stackoverflow.com/questions/986006/…
标签: function loops for-loop dictionary iteration