I would like to use dictionary value of the key to verify if condition is true eg
dictionary = [{'Class': '>50K'},{'Class': '<=50K'}]
for i in dictionary:
if i['Class'] == '<=50k':
po_list_count +=1
else:
rich_list_count +=1
print(po_list_count,'---',rich_list_count)
print should be 1 --- 1 instead I get 0 --- 2
I have tried with if i['Class'] in ['<=50k']
but same result.
Is it something I do not understand about how if statement works or perhaps how it treats strings?
You need to check for a capital "K":
if i['Class'] == '<=50K':
You used one in dictionary
:
dictionary = [{'Class': '>50K'},{'Class': '<=50K'}]
# here--^
在你的定义中,你使用大写<=50K
并在你的if语句中使用小写k
Because k
and K
is not the same character. You'll have to match it:
if i['class'] = '50K':
Or, a better trick: Just check both in lowercase!
if i['class'].lower() = '50k':
Hope this helps!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.