从字典里得到国家和州?

2024-05-15 16:28:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我对Python还不熟悉。我有一个dict看起来像这样。你知道吗

a = {'StartupName': 'FPL Technologies', 'coun': 'India', 'stat': 'Punjab', 'Amount': 4500000}

dict中,我想将country_typestate_typeint_typeall_type作为一个单独的列表。你知道吗

在上述dict的coun键中,它有国家名称,在stat键中,它有州名称。 程序应该检查dict中的值是否具有country name,然后应该将具有country值的键附加到country_type列表中。 如果dict中的值有一个state name,那么它应该将具有状态值的键附加到state_type列表中。你知道吗

请注意,“国家”是指所有国家,而不仅仅是印度。你知道吗

我不知道怎么取回它。有没有python中的州和国家的库,我可以用它来匹配数据?我知道pycountry库,但我不知道州。你知道吗

帮我找到一些解决这个问题的办法。你知道吗

以下是我尝试的代码:

a = {'StartupName': 'FPL Technologies', 'coun': 'India', 'stat': 'Punjab', 'Amount': 4500000}
all_type = []
country_type = []
state_type = []
int_type=[]
for i in a:
    print(i)
    if type(a[i]) == type(0):
        int_type.append(i)
print("Int Type:", int_type)
print("Country Type:",country_type) 
print("State Type:",state_type)
print("All Type:",all_type)

所需输出:

Int Type: ['Amount']
Country Type: ['coun']
State Type: ['stat']
All Type: ['StartupName','coun','stat,'Amount']

Tags: 列表type国家allamountcountrydictfpl