新行登录列表 Python

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20540521/
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 20:42:08  来源:igfitidea点击:

New line sign in list Python

pythonlistnewline

提问by Iliya Vaitzman

I'm trying to do an array of arrays in python but without success, so I thought it would be a lot easier for me to work with a list with lines breaks.

我正在尝试在 python 中做一个数组数组,但没有成功,所以我认为处理带有换行符的列表会容易得多。

What I'm doing is this :

我正在做的是:

board = []
for i in range(10):
    for j in range(10):
        board.append("0 ")
    board.append("\n")
print board

But when printing this , it comes out

但是当打印这个时,它出来了

['0 ', '0 ', '0 ', '0 ', '0 ', '0 ', '0 ', '0 ', '0 ', '0 ', '\n', '0 ', '0 ', '0 ', '0 ', '0 ', '0 ', '0 ', '0 ', '0 ', '0 ', '\n', '0 ', '0 ', '0 ', '0 ', '0 ', '0 ', '0 ', '0 ', '0 ', '0 ', '\n', '0 ', '0 ', '0 ', '0 ', '0 ', '0 ', '0 ', '0 ', '0 ', '0 ', '\n', '0 ', '0 ', '0 ', '0 ', '0 ', '0 ', '0 ', '0 ', '0 ', '0 ', '\n', '0 ', '0 ', '0 ', '0 ', '0 ', '0 ', '0 ', '0 ', '0 ', '0 ', '\n', '0 ', '0 ', '0 ', '0 ', '0 ', '0 ', '0 ', '0 ', '0 ', '0 ', '\n', '0 ', '0 ', '0 ', '0 ', '0 ', '0 ', '0 ', '0 ', '0 ', '0 ', '\n', '0 ', '0 ', '0 ', '0 ', '0 ', '0 ', '0 ', '0 ', '0 ', '0 ', '\n', '0 ', '0 ', '0 ', '0 ', '0 ', '0 ', '0 ', '0 ', '0 ', '0 ', '\n']

I dont want the '\n'to show as '\n', I want it to do a new line in the list.

我不希望'\n'显示为'\n',我希望它在列表中换行。

How can I do it ?

我该怎么做 ?

Thank you in advance, Iliya :)

提前谢谢你,伊利亚:)

采纳答案by Ffisegydd

If you want an array of arrays (or in this case a list of lists) filled with 0's then you could use:

如果你想要一个用 0 填充的数组数组(或者在这种情况下是一个列表列表),那么你可以使用:

board = [[0]*10 for i in range(10)] # Generate 10 lists filled with 10 0's each.

for x in board:
    print x # Print out each [0,0,...0,0] on a new line.

回答by Ignacio Vazquez-Abrams

Certainly not by printing the representationof the list...

当然不是通过打印列表的表示...

print ''.join(board)

回答by Tushar

You are printing a List. It will look as it is. "\n" will work if you print it as a string. Please try

您正在打印一个列表。它将看起来如此。如果您将其打印为字符串,“\n” 将起作用。请尝试

print "".join(board)

打印 "".join(board)

回答by Vb407

If u want to see a new line use list of lists don't append '\n'

如果您想查看新行,请使用列表列表不要附加 '\n'

Here is the sample:

这是示例:

board = []
for i in range(10):
    temp = []
    for j in range(10):
        temp.append("0 ")
    board.append(temp)

for i in board:
    print ''.join(i)

回答by eyquem

As usual, Ignacio didn't try to be more than laconic.

像往常一样,伊格纳西奥并没有试图做到简洁。

The reason why you obtain such a display as the one you show is that when a printstatement is done to print an object obj, the method obj.__str__is called to obtain a string that can be written on the display.
Then, in your case, it is board.__str__()which is called.

之所以获得如此显示的显示,是因为当执行print语句打印对象时objobj.__str__会调用该方法以获取可以写入显示的字符串。
然后,在您的情况下,它是board.__str__()被调用的。

The fact is that the element '\n'is a character but all the elements '\n'in boardare not elements of a string : they are elements of a list and __str__is such that the elements of a list are represented inside a list as strings obtained from __repr__(see the doc on this method and its difference with __str__)
Understanding more precisely the process executed by the printstatement would require to examine the code of the statement print, but I think it isn't in Python code but in C code (I suppose, I'm not very competent on this point)

事实是,该元件'\n'是一个字符,但所有的元素'\n'board没有字符串的元素:它们是一个列表的元素和__str__是这样的列表的元素列表作为由获得的字符串内表示 __repr__(见该文档这种方法和它的区别__str__)
更准确地理解print语句执行的过程需要检查语句的代码print,但我认为它不是在 Python 代码中,而是在 C 代码中(我想,我不是很能干在这一点上)

That said, I think that what you want to obtain is a display like that:

也就是说,我认为您想要获得的是这样的显示:

0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0

There's no need to place some '\n'characters in a list to obtain this display. Rather, do this:

无需'\n'在列表中放置一些字符即可获得此显示。而是这样做:

board = []
for i in range(100):
    board.append('0 ')
# or better: board = ['0 ' for i in range(100)]
print '\n'.join(''.join(board[i:i+10]) for i in xrange(0,100,10))

print

board[6] = '4 '
board[18] = 'A '
board[30] = '9 '
board[77] = 'X '
print '\n'.join(''.join(board[i:i+10]) for i in xrange(0,100,10))

result

结果

0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 

.

.

0 0 0 0 0 0 4 0 0 0 
0 0 0 0 0 0 0 0 A 0 
0 0 0 0 0 0 0 0 0 0 
9 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 X 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0

.

.

NB
In Python an arrayobject is an object of this type
The elements of an array have all the same constrained type which can't be the array's type. So an object being an array of array means nothing in Python.

注意
在 Python 中,array对象是这种类型的对象
数组的元素都具有相同的约束类型,不能是array的类型。因此,作为数组数组的对象在 Python 中没有任何意义。

NB2
Now with the above definition of board, you have the problem that elements can be only strings of length 2 with a blank at the second character.
There are solutions to manage situations with elements being not strings, and of variable length, but it becomes rapidly complicated.
That's why the Numpylibrary has been created, to allow the easy manipulation of array presentation thanks to a type called ndarray

NB2
现在有了上面的 定义board,你会遇到一个问题,即元素只能是长度为 2 的字符串,第二个字符处有一个空格。
有一些解决方案可以管理元素不是字符串且长度可变的情况,但它会变得非常复杂。
这就是为什么创建了Numpy库的原因,它允许通过名为的类型轻松操作数组表示ndarray