Python 不在for dict()中不起作用
这是我的密码。我只想在名为“country”的字典中记录键时的值,该值在字典中不存在。为此,我正在检查关键字notin(代码的最后一行)。但是,它不起作用。即使字典中存在键,它也会更新值。任何帮助都将不胜感激Python 不在for dict()中不起作用,python,python-3.x,Python,Python 3.x,这是我的密码。我只想在名为“country”的字典中记录键时的值,该值在字典中不存在。为此,我正在检查关键字notin(代码的最后一行)。但是,它不起作用。即使字典中存在键,它也会更新值。任何帮助都将不胜感激 df = pd.DataFrame() country = dict() for file in allFiles: df = pd.read_excel(file,'Internal', skiprows = 7) print ("file name is " + fil
df = pd.DataFrame()
country = dict()
for file in allFiles:
df = pd.read_excel(file,'Internal', skiprows = 7)
print ("file name is " + file)
if(df.loc[1][0] == "Country:"):
key = df.loc[1][1]
value = df.loc[2][1]
else:
key = df.loc[2][1]
value = df.loc[1][1]
print ('key is ' , key)
print ('value is ' , value)
try:
print (df.loc[47][2], df.loc[47][3])
print (df.loc[49][2], df.loc[49][3])
except Exception as inst:
print (type(inst))
print (inst.args)
print (inst)
if ((key not in country) or (value != 'nan')):
country[key] = value
改变你的状况
# Even if 'key' was already in 'country', If (value != 'nan')
# evaluated to 'True', the update will happen
if ((key not in country) or (value != 'nan')):
到
由于
值
很可能不是nan
,因此记录将始终更新。从
# Even if 'key' was already in 'country', If (value != 'nan')
# evaluated to 'True', the update will happen
if ((key not in country) or (value != 'nan')):
到
由于
value
很可能不是nan
,因此记录将始终更新。请阅读if语句中的条件,并思考如果value
不是nan会发生什么。请阅读if语句中的条件,并思考如果value
不是nan会发生什么。