在不带numpy的Python中创建矩阵 [英] Creating a Matrix in Python without numpy

查看:112
本文介绍了在不带numpy的Python中创建矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建和初始化矩阵.我遇到的问题是,我创建的矩阵的每一行都是相同的,而不是遍历数据集. 我试图通过检查值是否已经在矩阵中来纠正它,但这不能解决我的问题.

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)

输出应如下所示: ['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 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']

我的问题是,我将仅获得返回的所有5个列表的第一个a,b,c,d,e列表

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

推荐答案

您需要跟踪循环中的当前索引.

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

基本上,您希望将像0、1、2、3、4,.... 24(这些是初始数组的索引,alpha)的列表转换为:

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 ...等

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()

然后将其打印出来:

[['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']]

之所以总是收到a,b,c,d,e的原因是因为在编写此代码时:

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

        rowList.append(dataList[j])

它正在有效地做的是对每一行进行0-4的迭代.所以基本上:

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

这篇关于在不带numpy的Python中创建矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆