【发布时间】:2017-03-05 08:26:36
【问题描述】:
文件包含:
3
7 4
2 4 6
8 5 9 3
输出:[[3],[7,4],[2,4,6],[8,5,9,3]]
我的输出:
[[3], [7, 4], [2, 4, 6], [8, 5, 9, 3]]
^ ^ ^ // don't want these spaces
我的解决方案:
def read_triangle(filename):
f = open(filename,'r')
triangle = []
for line in f:
splited = line.split()
triangle.append([])
for num in splited:
triangle[-1].append(int(num))
f.close()
return triangle
我必须怎么做才能删除空格?
【问题讨论】:
-
到底是什么问题?
-
我的输出:[[3], [7, 4], [2, 4, 6] :(每个索引之间和索引内部都有一个空格。但输出必须是没有任何空格。
-
完全相同的列表。你确定你需要像那样把它全部打印出来吗?
-
如何打印列表?你在用
print(read_triangle(filename)) -
@HalaTwin 这与列表本身无关。它与默认的打印方式有关。
标签: python list file split integer