[问题] list与dict的混用

楼主: cococrisp7 (LIVEBA)   2020-07-22 20:56:56
大家好, 小弟目前在正在做爬虫程式, 遇到了一个lis与dict互用的问题, 撷取程式的其

countries = []
country_dict = {'us':'United States', 'de':'Germany', 'fr':'France', 'jp':'Jap
an', 'es':'Spain', 'gb':'Great}
while True:
country_input = input('Please enter the countries (or type q to exit)
:
if country_input == 'q':
break
elif country_input in list(country_dict.keys()):
countries.append(country_input)
elif country_input not in list(country_dict.keys()):
print('Country Not Found, please retry again !!')
print(countries)
假如输入 us, de => 会印出['us', 'de']
如果想要输出对应到country_dict的value
让输出变成['United States', 'Germany'], 应该要怎么修改呢?
countries=[] 这个list需要保留,因为之后的爬虫网址会搭配用到us, de这些缩写来做
循环
想了很久还是想不出来, 希望有人可以指点一下~感谢!
作者: papple23g (逆道者)   2020-07-22 21:39:00
countries.append(country_dict[country_input])
作者: OnoderaHaru (小野寺春)   2020-07-22 21:40:00
print([country_dict[country] for country incountries])
作者: refusekkk (另类3k)   2020-07-23 10:23:00
2F正解 1F应该是没看到爬虫网址会搭配用到us, de

Links booklink

Contact Us: admin [ a t ] ucptt.com