Cartopy-Python 语法 - 一行中有多个 objects/countries

Cartopy-Python syntax - multiple objects/countries in one line

我正在学习这个例子,Python Mapping in Matplotlib Cartopy Color One Country。它与几个国家全面合作,例如美国、法国、英国、日本。

for country in countries:
    if country.attributes['adm0_a3'] == 'USA':
        ax.add_geometries(country.geometry, ccrs.PlateCarree(),
                          facecolor='#008744', alpha = 0.5,
                          label=country.attributes['adm0_a3']),

    if country.attributes['adm0_a3'] == 'FRA':
        ax.add_geometries(country.geometry, ccrs.PlateCarree(),
                          facecolor='#008744', alpha = 0.5,
                          label=country.attributes['adm0_a3']),
+ 'GBR'
+ 'JPN'

else:
    ax.add_geometries(country.geometry, ccrs.PlateCarree(),
                      facecolor=('#c4e6ff'),
                      label=country.attributes['adm0_a3'])

我想将国家列表放在一行中,而不是一遍又一遍地重复陈述。

我试过了:

if country.attributes['adm0_a3'] == ['USA', 'FRA', 'GBR', 'JPN']:

any('USA, 'FRA', 'GBR', 'JPN')

['USA or 'FRA' or 'GBR' or'JPN']

还有一条口述:

myDict = {'USA', 'FRA', 'GBR', 'JPN'}
if country.attributes['adm0_a3'] == myDict:

显然,我的逻辑不太正确。

你应该使用 in 关键字,像这样:

for country in countries:
    if country.attributes['adm0_a3'] in ['USA', 'FRA', 'GBR', 'JPN']:
        ax.add_geometries(country.geometry, ccrs.PlateCarree(),
                          facecolor=(0, 0, 1),
                          label=country.attributes['adm0_a3'])
    else:
        ax.add_geometries(country.geometry, ccrs.PlateCarree(),
                          facecolor=('#c4e6ff'),
                          label=country.attributes['adm0_a3'])

这就是你要找的吗?