如何从不同的py-fi返回项目列表

2024-10-01 22:40:18 发布

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

我有2个.py文件(data.py,access.py)

access.py中,我有几个字符串:

Japan = ['brown', 'cow', 'around']
Korea = ['black', 'chicken', '344']
China = ['grey', '3411', 'leaf']
Indonesia = ['red', 'green', '']

data.py中,我有一个模块:

def whichcolour(country)
       print("access + country[1]")  # Doesn't work

我的结果

我试图实现的是,运行whichcolourdata.py,它将返回/打印列表中的任何内容

我知道print(access.Japan[1])有用。。它返回cow

如何编写print语句,使其得到相同的结果


Tags: 文件字符串pydataaccesscountryaroundblack
1条回答
网友
1楼 · 发布于 2024-10-01 22:40:18

使用字典

访问.py

countries = {
    'Japan': ['brown', 'cow', 'around'],
    'Korea': ['black', 'chicken', '344'],
    'China': ['grey', '3411', 'leaf'],
    'Indonesia': ['red', 'green', '']
}

数据.py

from access import countries

def whichcolour(country, countries):
    print('{} has the color {}'.format(country, countries[country][0]))

for country in countries:
    whichcolour(country, countries)

输出:

Japan has the color brown
Korea has the color black
China has the color grey
Indonesia has the color red

我不是100%确定你想做什么(印尼有两种颜色),但这应该让你开始

相关问题 更多 >

    热门问题