带新行的 Python 打印数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13893399/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 09:52:33  来源:igfitidea点击:

Python print array with new line

python

提问by

I'm new to python and have a simple array:

我是 python 新手,有一个简单的数组:

op = ['Hello', 'Good Morning', 'Good Evening', 'Good Night', 'Bye']

When i use pprint, i get this output:

当我使用 pprint 时,我得到以下输出:

['Hello', 'Good Morning', 'Good Evening', 'Good Night', 'Bye']

Is there anyway i can remove the quotes, commas and brackets and print on a seperate line. So that the output is like this:

无论如何我可以删除引号,逗号和括号并在单独的行上打印。所以输出是这样的:

Hello
Good Morning
Good Evening
Good Night
Bye

回答by Jon Clements

Print it line by line

一行一行打印

for word in op:
    print word

This has the advantage that if ophappens to be massively long, then you don't have to create a new temporary string purely for printing purposes.

这样做的好处是,如果op碰巧很长,那么您不必纯粹出于打印目的创建新的临时字符串。

回答by Sebastian Paaske T?rholm

You could jointhe strings with a newline, and print the resulting string:

您可以join使用换行符来显示字符串,并打印结果字符串:

print "\n".join(op)

回答by NlightNFotis

You seem to be confused. Let me help you clarify some things in your mind =)

你好像糊涂了。让我帮你理清你心中的一些事情=)

  1. First of all what you have there is a list, not an array. The difference is that the list is a far more dynamic and flexible data structure (at list in dynamic languages such as python). For instance you can have multiple objects of different types (e.g have 2 strings, 3 ints, one socket, etc)
  2. The quotes around the words in the list denote that they are objects of type string.
  3. When you do a print op(or print(op)for that matter in python 3+) you are essentially asking python to show you a printable representation of that specific list object and its contents. Hence the quotes, commas, brackets, etc.
  4. In python you have a very easy for eachloop, usable to iterate through the contents of iterable objects, such as a list. Just do this:
  1. 首先,你拥有的是一个list,而不是一个数组。不同之处在于列表是一种更加动态和灵活的数据结构(at list 在动态语言如 python 中)。例如,您可以拥有多个不同类型的对象(例如,有 2 strings、3 ints、1socket等)
  2. 列表中单词周围的引号表示它们是 type 的对象string
  3. 当你做一个print op(或者print(op)在 python 3+ 中)时,你本质上是在要求 python 向你显示该特定列表对象及其内容的可打印表示。因此,引号,逗号,括号等。
  4. 在 python 中,您有一个非常简单的for each循环,可用于遍历可迭代对象的内容,例如list. 只需这样做:

for greeting in op: print greeting

for greeting in op: print greeting

回答by Ryan Chu

For Python 3, we can also use list unpack
https://docs.python.org/3.7/tutorial/controlflow.html#unpacking-argument-lists

对于 Python 3,我们还可以使用列表解包
https://docs.python.org/3.7/tutorial/controlflow.html#unpacking-argument-lists

print(*op, sep='\n')

It's the same as

它与

print('Hello', 'Good Morning', 'Good Evening', 'Good Night', 'Bye', sep='\n')