3

In Python, matrices can be created using nested lists. For example, [[1, 2], [3, 4]]. Below I have written a function that prompts the user for the dimensions of a square matrix and then prompts the user for the values in the for loop. I have a tempArray variable that temporarily stores one row of values, and then is deleted after it is appended to the matrix array. For some reason, when I print the matrix at the end, this is what I get: [ [ ], [ ] ]. What is going wrong?

def proj11_1_a():
    n = eval(input("Enter the size of the square matrix: "))
    matrix = []
    tempArray = []   

    for i in range(1, (n**2) + 1):
        val = eval(input("Enter a value to go into the matrix: "))

        if i % n == 0:
            tempArray.append(val)
            matrix.append(tempArray)
            del tempArray[:]
        else:
            tempArray.append(val)
    print(matrix)
proj11_1_a()
4
  • there is no printing in this code Commented Dec 5, 2015 at 23:21
  • Sorry I deleted that line Commented Dec 5, 2015 at 23:22
  • why do you delete your array? del tempArray[:] Commented Dec 5, 2015 at 23:23
  • the array is only deleted after it has one row of values. The reason why it is deleted is so it can then store the next row of values and then append it to the matrix array. Commented Dec 5, 2015 at 23:25

2 Answers 2

2

You simply delete the array elements del tempArray[:] and as lists are mutable it also clears part of matrix

def proj11_1_a():
    n = eval(input("Enter the size of the square matrix: "))
    matrix = []
    tempArray = []   

    for i in range(1, (n**2) + 1):
        val = eval(input("Enter a value to go into the matrix: "))

        if i % n == 0:
            tempArray.append(val)
            matrix.append(tempArray)
            tempArray = [] #del tempArray[:]
        else:
            tempArray.append(val)
    print(matrix)
proj11_1_a()

Which could be further simplified/cleared to

def proj11_1_a():
    # Using eval in such place does not seem a good idea
    # unless you want to accept things like "2*4-2"
    # You might also consider putting try: here to check for correctness

    n = int(input("Enter the size of the square matrix: "))
    matrix = []

    for _ in range(n): 
        row = []   

        for _ in range(n): 
            # same situation as with n
            value = float(input("Enter a value to go into the matrix: "))
            row.append(value)

        matrix.append(row)

    return matrix
Sign up to request clarification or add additional context in comments.

3 Comments

you will be doing the append(val) anyways so it is better to get it out from the if/else blocks
@Jjpx - it is OP code, only fragment causing the problem was changed, there are many things here which could be improved
As @Jjpx suggested, you could provide optimal version as well. I think it would be beneficial to the OP to have both versions.
1

Another solution is to change the following line:

matrix.append(tempArray)

to:

matrix.append(tempArray.copy())

3 Comments

which is suboptimal as you need to ... copy the object (thus if it is large, it takes linear time).
As the original intention was to prompt user for the input, overhead is minimal compared to the speed of the user input.
Sure, but it does not mean one should write insufficient code elsewhere. Neither should we suggest it to people learning python (like OP)

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.