Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从用户输入打印列表_Python_Python 3.x_List_Printing - Fatal编程技术网

Python 从用户输入打印列表

Python 从用户输入打印列表,python,python-3.x,list,printing,Python,Python 3.x,List,Printing,如何为此编写打印命令? 感谢您的帮助:有两种方法,一种是制作包含所有可供打印列表的词典,另一种是使用不可打印的eval 或 两个代码段的输出: List1 = [1,2,4,4] List2 = [2,4,4] List3 = [1,7] text = input("input name of list to be printed: ") print(eval(text)) 此代码打印指定列表的每个元素 列表1=[1,2,4,4] 列表2=[2,4,4] 列表3=[1,7

如何为此编写打印命令?
感谢您的帮助:

有两种方法,一种是制作包含所有可供打印列表的词典,另一种是使用不可打印的eval

两个代码段的输出:

List1 = [1,2,4,4]
List2 = [2,4,4]
List3 = [1,7]

text = input("input name of list to be printed: ")
print(eval(text))

此代码打印指定列表的每个元素

列表1=[1,2,4,4] 列表2=[2,4,4] 列表3=[1,7] 列表=输入要打印的列表的名称: 如果List==Lists1: 对于列表1中的i: 普林蒂 elif List==Lists2: 对于列表2中的i: 普林蒂 elif List==Lists3: 对于列表1中的i: 普林蒂 其他: 打印列表不存在
您可以通过在dict中插入它们来实现这一点

input name of list to be printed: List1
[1, 2, 4, 4]

我会制作一本字典,这样你就可以打印出所需密钥的值。嗨,欢迎使用so,请阅读以及如何创建和编辑你的问题,以及你想要实现的目标和你的尝试code@Austin同意的词典绝对是解决这个问题的方法,请重复并从下面开始。“演示如何解决此编码问题”不是堆栈溢出问题。你必须做出诚实的尝试,然后就你的算法或技术提出一个具体的问题。当你提议使用dict时,没有必要使用dicteval@MauriceMeyer我不确定询问者想要什么,所以我把这两种方式都包括在内。Eval对一些用户来说似乎很简单,但它是否存在安全风险?我认为使用Eval更为复杂,因为字典还可以将列表中包含的数据分组在一起(如果它们相关),并提供其他有用的方法和例外。然而,eval确实可以完成这项工作。@leonisum我同意对于更大的代码段,eval肯定会阻碍开发甚至测试过程。我也更喜欢dict方法。我把eval作为一个例子来说明可以做什么,但也许不应该做什么;
List1 = [1,2,4,4]
List2 = [2,4,4]
List3 = [1,7]

text = input("input name of list to be printed: ")
print(eval(text))
input name of list to be printed: List1
[1, 2, 4, 4]
list_dict = {"Lists1":[1,2,4,4],
"Lists2":[2,4,4],
"Lists3" :[1,7]
}
List = input("input name of list to be printed: ")
print(list_dict.get(List))
#[1,2,4,4]