我正在编写一个函数来清理字典数据,其中包含一个键值对,显示日期和该日期的降雨量。数据清洗的条件需要去除和键值对满足以下条件的值:
def clean_data (adic):
newDic = {}
for (date,inches) in adic.items():
print (date,inches)
if not type(inches) == float:
if not type(inches) == int:
print ("type")
continue
elif inches < 0:
print ("below 0")
continue
elif inches > 100:
print ("above 100")
continue
else:
print ("added")
newDic[date]=inches
return newDic
rainfall = {"20190101": 5, "20190102": "6", "20190103": 7.5,
"20190104": 0, "20190105": -7, "20190106": 102,
"20190107": 1}
print(clean_data(rainfall))
Run Code Online (Sandbox Code Playgroud)
出于某种原因,我的代码正在输出:
20190101 5
20190102 6
type
20190103 7.5 …Run Code Online (Sandbox Code Playgroud)