如何根据字符串输入打印列表名称?



我想调用list依赖于字符串输入

代码示例:
day = [1,7]
month = [1,12]
ip = input("day or month :")
print(
#example the input is "day", i want to print day[list]/ variable declared before
) 

我们可以用

if p == "day":
print(day) 

和月但如果我们有更多的性质,这需要很多时间。什么是最有效的方法来打印列表依赖于字符串输入?

使用字典!

my_dict_of_lists = {
'day': list(range(1, 32)),
'month': list(range(1, 13)),
}
print(my_dict_of_lists['day'])

最新更新