Python 返回文件中的变量
我有法国、德国的元组变量。我试图为我的 Bring_cities 函数赋予一个值,如果是法国或德国,我喜欢查看法国和德国元组对象。有没有像我在下面所做的那样不使用 if 循环的快捷方式?
France = (
('Paris', 'Paris'),
('Lyon', 'Lyon'),
)
Germany = (
('Frankfurt', 'Frankfurt'),
('Berlin', 'Berlin'),
)
cities = (('', ''),) + France + Germany
def bring_cities(country):
if country == 'France':
return France
if country == 'Germany':
return Germany
...
I have tuple variables which are France, Germany. I'm trying to give a value to my bring_cities function and if it's France or Germany, I like to see the France and Germany tuple objects. Is there any shortcut to not use if loops like I did in the below ?
France = (
('Paris', 'Paris'),
('Lyon', 'Lyon'),
)
Germany = (
('Frankfurt', 'Frankfurt'),
('Berlin', 'Berlin'),
)
cities = (('', ''),) + France + Germany
def bring_cities(country):
if country == 'France':
return France
if country == 'Germany':
return Germany
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以创建一个字典,这样就不必为每个国家/地区编写 if 语句。您可以使用以下代码来实现此目的。
为了使其更短,您可以在字典中定义您的国家/地区。
You can create a dictionary so you don't have to write an if statement for each country. You can use the following code for that.
to make it even shorter you can define your Countries inside the dictionary.
恢复格尔达的回答
Resuming gerda's answer