python元组元素的提取_从python中的元组中获取元素

COUNTRIES = (

('AF', (u'Afghanistan')),

('AX', (u'\xc5land Islands')),

('AL', (u'Albania')),

('DZ', (u'Algeria')),

('AS', (u'American Samoa')),

('AD', (u'Andorra')),

('AO', (u'Angola')),

('AI', (u'Anguilla'))

)

print (country for (code, country) in COUNTRIES if code=='AD').next()

#>>> Andorra

print next((country for (code, country) in COUNTRIES if code=='AD'), None)

#Andorra

print next((country for (code, country) in COUNTRIES if code=='Blah'), None)

#None

# If you want to do multiple lookups, the best is to make a dict:

d = dict(COUNTRIES)

print d['AD']

#>>> Andorra

文档信息

版权声明:可自由转载(请注明转载出处)-非商用-非衍生

发表时间:2021年12月9日 15:17