基于标题,帮我解决这个错误。 我尝试根据'rv'变量中的country_name打印countryCode。 country_found是一个具有相同值的国家列表数据, 然后我尝试检索countryCode,结果出现了错误。
rv = "Indonesia"
country_lower = rv.lower()
countries = {
"DATA": {
"data": [{
"countryId": "26",
"countryCode": "AU",
"name": "Australia"
}, {
"countryId": "17",
"countryCode": "ID",
"name": "Indonesia"
}]
}
}
def take_first(predicate, iterable):
for element in iterable:
if predicate(element):
yield element
break
country_found = list(
take_first(
lambda e: e['name'].lower() == country_lower,
countries['DATA']['data']
)
)
default_country_code = 'US'
country_code = (
country_found['countryCode']
if country_found
else default_country_code
)
print (country_code)