[英]How do I print elements in a Python list one by one?
If I have a list in Python like this:如果我在 Python 中有这样的列表:
x = ["1","2","3","4","5"]
...how can I get it to print 1 and then 2 and then 3 and so on and so forth? ...我怎样才能让它打印 1 然后 2 然后 3 等等等等?
Although this sounds suspiciously like a homework assignment you're asking for help with, you could do this using a For loop:尽管这听起来有点像您正在寻求帮助的家庭作业,但您可以使用 For 循环来执行此操作:
for num in x:
print(num)
You iterate through the list using a For loop, and print each element in the list which becomes a step in the iteration process.您使用 For 循环遍历列表,并打印列表中的每个元素,这成为迭代过程中的一个步骤。
Link: https://www.w3schools.com/python/python_for_loops.asp链接: https://www.w3schools.com/python/python_for_loops.asp
You could use the sep= parameter of the print function with unpacking and a new line as separator value:您可以使用 print function 的 sep= 参数进行解包,并以新行作为分隔符值:
print(*x,sep="\n")
1
2
3
4
5
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.