简体   繁体   English

如何一一打印 Python 列表中的元素?

[英]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 a for loop, explained here .您可以使用for循环,在此处解释。

for y in x:
    print(y)

Use for loop .使用for 循环

for number in x:
     print(number)

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.

相关问题 如何打印一个列表中另一个列表中的元素? - How do I print elements of one list that are in another list? 我想使用python中的列表理解一一打印列表中的项目。 怎么能这样做? - I want print items in list one by one using list comprehension in python. How can do that? 如何确定一个列表是否包含另一个列表中的项目,然后在 Python 中打印它们的索引 - how do I determine if one list has items contained in another list, then print their index in Python 如何将一个列表的元素放在另一个列表的中间? 一维战舰游戏。 (蟒蛇) - How do I place the elements of a list into the middle of another list? A one dimensional battleship game. (Python) 如何在一行中打印列表元素? - How to print list elements in one line? 如何将一个数组的0、2、3、4个元素与另一个数组的0、2、3、4个元素匹配,并在python中从两个数组中打印出第5个元素? - How do I match 0,2,3,4 elements of one array to 0,2,3,4 elements of another array and print the 5th element from both the arrays in python? 如何检查一个列表是否包含另一个列表的 2 个元素? - How do I check if one list contains 2 elements of another list? 如何在Python 3中将列表中的所有元素仅转换为一个int? - How do I turn all elements on a list into only one int in Python 3? Python:如何将列表的一个元素与除自身之外的所有其他元素进行比较? - Python: How do I compare one element of a list with every other elements besides itself? 如何在python shell中一次打印一个单词? - How do I print one word at a time in python shell?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM