作者cococrisp7 (LIVEBA)
看板Python
标题[问题] list与dict的混用
时间Wed Jul 22 20:56:56 2020
大家好, 小弟目前在正在做爬虫程式, 遇到了一个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这些缩写来做
回圈
想了很久还是想不出来, 希望有人可以指点一下~感谢!
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 27.52.199.191 (台湾)
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Python/M.1595422618.A.0EA.html
1F:推 papple23g: countries.append(country_dict[country_input]) 07/22 21:39
2F:推 OnoderaHaru: print([country_dict[country] for country in 07/22 21:40
3F:→ OnoderaHaru: countries]) 07/22 21:40
4F:→ refusekkk: 2F正解 1F应该是没看到爬虫网址会搭配用到us, de 07/23 10:23