-3

The file contains:

3
7 4
2 4 6
8 5 9 3

output: [[3],[7,4],[2,4,6],[8,5,9,3]]

my output:

 [[3], [7, 4], [2, 4, 6], [8, 5, 9, 3]]    
      ^       ^          ^             // don't want these spaces

my solution:

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

what do i have to do to remove spaces?

Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
Hala Twin
  • 1
  • 1

1 Answers1

1

Your output is fine. You have the correct list. The required output only appears without spaces because whoever typed it was too lazy to hit the spacebar (I'm guilty of that from time to time, as well). You could get that output with str(result).replace(' ', ''), but, again, it is not necessary. The Python interpreter's default representation of a list object simply includes spaces for readability.

Nothing is wrong.

TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97