Python将新行附加到数组中的字符串

时间:2015-11-10 13:59:00

标签: python arrays string append newline

将新行添加到数组中的字符串

result = []

str = ""

str = str + " this is my result for..."

result.append([x, y, str])

输出将是:

[1, 2, this is my result for...], [12, 297, this is my result for...], [13, 42, this is my result for...], [34, 42, this is my result for...],[1345, 332, this is my result for...], [341, 244, this is my result for...]

但我希望输出为:

[1, 2, this is my result for...], 
[12, 297, this is my result for...], 
[13, 42, this is my result for...], 
[34, 42, this is my result for...],
[1345, 332, this is my result for...], 
[341, 244, this is my result for...]

1 个答案:

答案 0 :(得分:2)

您可以使用pprint来获取格式化的输出。

result = [
    ['this is my result for...', 1, 2],
    ['this is my result for...', 12, 297],
    ['this is my result for...', 13, 42]]

from pprint import pprint
pprint result
  

[['这是我的结果......',1,2],
   ['这是我的结果...',12,297],
   ['这是我的结果......',13,42]]

或者按照jonrsharpe的建议循环结果:

result = [
    ['this is my result for...', 1, 2],
    ['this is my result for...', 12, 297],
    ['this is my result for...', 13, 42]]

for line in result: print line
  

['这是我的结果......',1,2]
   ['这是我的结果...',12,297]
   ['这是我的结果......',13,42]