如何在Python中仅使用列表而不使用词典将城市与国家关联起来?

2024-10-06 15:29:16 发布

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

我有两个城市和国家的名单。你知道吗

List1=['Athens', 'Sidney']

List2=['Greece', 'Australia']

c=raw_ input('Enter a city: ')

雅典在希腊,悉尼在澳大利亚。你知道吗

我怎样才能用列表而不是字典来测试西德尼是否在澳大利亚?你知道吗


Tags: city列表inputraw字典国家enter名单
2条回答

zip两个列表,组成一个字典,检查悉尼钥匙!你知道吗

myHash=dict(zip(List1, List2))
myHash.has_key("Sidney")

打印True如果存在,否则False

你可以这样做:

result = [country for city,country in zip(List1,List2) if city == c]
if result:
    print('This city is in {}'.format(result[0]))
else:
    print('City not found')

相关问题 更多 >