Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a Matrix in Python without numpy [duplicate]

Tags:

python

matrix

I'm trying to create and initialize a matrix. Where I'm having an issue is that each row of my matrix I create is the same, rather than moving through the data set. I've tried to correct it by checking if the value was already in the matrix and that didn't solve my problem.

def createMatrix(rowCount, colCount, dataList):   
    mat = []
    for i in range (rowCount):
        rowList = []
        for j in range (colCount):
            if dataList[j] not in mat:
                rowList.append(dataList[j])
        mat.append(rowList)

    return mat 

def main():  
    alpha = ['a','b','c','d','e','f','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
    mat = createMatrix(5,5,alpha)
    print (mat)

The output should be like this: ['a','b','c','d','e'] , ['f','h','i','j','k'], ['l','m','n','o','p'] , ['q','r','s','t','u'], ['v','w','x','y','z']

My issue is I just keep getting the first a,b,c,d,e list for all 5 lists returned

like image 232
Sarah Allen Avatar asked Oct 19 '16 02:10

Sarah Allen


People also ask

How do you create an empty matrix in Python?

To create an empty matrix, we will first import NumPy as np and then we will use np. empty() for creating an empty matrix. After writing the above code (Create an empty matrix using NumPy in python), Once you will print “m” then the output will appear as a “ [ ] ”.

How do you create a matrix using numpy in Python?

We can create a matrix in Numpy using functions like array(), ndarray() or matrix(). Matrix function by default creates a specialized 2D array from the given input. The input should be in the form of a string or an array object-like.

How do you create a matrix in a list in Python?

In Python, we can implement a matrix as a nested list (list inside a list). We can treat each element as a row of the matrix. For example X = [[1, 2], [4, 5], [3, 6]] would represent a 3x2 matrix. First row can be selected as X[0] and the element in first row, first column can be selected as X[0][0] .


1 Answers

You need to keep track of the current index in your loop.

Essentially you want to turn a list like 0,1,2,3,4,....24 (these are the indices of your initial array, alpha) into:

R1C1, R1C2, R1C3, R1C4, R1C5 R2C1, R2C2... etc

I added the logic to do this the way you are currently doing it:

def createMatrix(rowCount, colCount, dataList):
    mat = []
    for i in range(rowCount):
        rowList = []
        for j in range(colCount):
            # you need to increment through dataList here, like this:
            rowList.append(dataList[rowCount * i + j])
        mat.append(rowList)

    return mat

def main():
    alpha = ['a','b','c','d','e','f','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
    mat = createMatrix(5,5,alpha)
    print (mat)

main()

which then prints out:

[['a', 'b', 'c', 'd', 'e'], ['f', 'h', 'i', 'j', 'k'], ['l', 'm', 'n', 'o', 'p'], ['q', 'r', 's', 't', 'u'], ['v', 'w', 'x', 'y', 'z']]

The reason you were always receiving a,b,c,d,e is because when you write this:

        rowList.append(dataList[j])

what it is effectively doing is it is iterating 0-4 for every row. So basically:

i = 0
rowList.append(dataList[0])
rowList.append(dataList[1])
rowList.append(dataList[2])
rowList.append(dataList[3])
rowList.append(dataList[4])
i = 1
rowList.append(dataList[0]) # should be 5
rowList.append(dataList[1]) # should be 6
rowList.append(dataList[2]) # should be 7
rowList.append(dataList[3]) # should be 8
rowList.append(dataList[4]) # should be 9

etc.

like image 168
enderland Avatar answered Oct 18 '22 10:10

enderland