0

I have this code wrote in python,used to input a matrix:

def gestionmatrice():       

    print("Entrez la matrice,séparé par des espaces pour chaques colonnes")
    print("une rangée par ligne,et faites un ligne vide pour terminer")
    matrix = []
    while True:
        line = input()
        if not line: break
        values = line.split()
        row = [int(value) for value in values]
        matrix.append(row)

    print(matrix)

The user input the matrix by entering all desired int value per row separated by a space,then press enter to confirm the row and input the next one. To finish, user have to input a blank row and press enter. At the end,I want to see the matrix by printing it; the problem is that it doesn't work correctly; if for example the user input 1 2 3 and then 4 5 6 on the next row, the print(matrix) will result as:[[1, 2, 3], [4, 5, 6]].It would be supposed to print 1 2 3 4 5 6 so something doesn't work.What has to be modified? Thanks in advance.

4
  • Could you make clear in the question body what you would expect the output to be? I tried to salvage it in my edit but I couldn't make it out. Commented Apr 12, 2014 at 20:00
  • actually I would like the output to display 1 2 3 and on the row under,4 5 6 aligned with the 1 2 3. Commented Apr 12, 2014 at 20:01
  • What happens when there's a 10? Do you want everything to be aligned? Commented Apr 12, 2014 at 20:16
  • In a perfect world,yes,but its also okay like this. Commented Apr 12, 2014 at 20:39

3 Answers 3

1

You can print out a matrix in Python like this:

def print_matrix(matrix):
    print('\n'.join([' '.join(map(str, row)) for row in matrix]))

Here's a get matrix function:

def get_matrix():
    matrix = []
    line = input()
    while line:
        matrix.append(map(int, line.split()))
        line = input()
    return matrix

Demo:

>>> matrix = get_matrix()
1 2 3
4 5 6
7 8 9

>>> print_matrix(matrix)
1 2 3
4 5 6
7 8 9
Sign up to request clarification or add additional context in comments.

Comments

0

I'm not sure quite why you want it to be like that, but you could do something like:

Instead of print(matrix), do:

>>> print(*(' '.join(map(str, sub)) for sub in matrix), sep='\n')
1 2 3
4 5 6

That is the quick fix.

1 Comment

@HaiVu Realised that pretty much as you posted, edited and thank you.
0
[[1, 2, 3], [4, 5, 6]]

is the default printing of a matrix (in many language, not only python).

To achieve what you want you have to do something like

import sys
for i in xrange(2):
    for j in xrange(3):
        sys.stdout.write(str(matrix[i,j] + " "))
    sys.stdout.write("\n")

2 Comments

oh really? I thought I was wrong since I thought [[1, 2, 3], [4, 5, 6]] was just like 2 list in a big list,and not a matrix.So its still a matrix,and its just normal that it result a display like this [[1, 2, 3], [4, 5, 6]]? Thanks
@Jon_Computer That's "a" way to represent matrices, but by itself python will never print a matrix like you originally wanted to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.