在没有 numpy 的情况下在 Python 中创建矩阵
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40120892/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating a Matrix in Python without numpy
提问by Sarah Allen
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']
输出应该是这样的: ['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
我的问题是我只是不断获得返回的所有 5 个列表的第一个 a、b、c、d、e 列表
采纳答案by enderland
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:
基本上你想把一个像 0,1,2,3,4,....24 这样的列表(这些是你的初始数组的索引,alpha)变成:
R1C1, R1C2, R1C3, R1C4, R1C5 R2C1, R2C2... etc
R1C1、R1C2、R1C3、R1C4、R1C5、R2C1、R2C2...等
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:
你总是收到的原因a,b,c,d,e
是因为当你写这个的时候:
rowList.append(dataList[j])
what it is effectively doing is it is iterating 0-4 for every row. So basically:
它有效地做的是对每一行迭代 0-4。所以基本上:
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.
等等。
回答by dawg
You can use a list comprehension:
您可以使用列表理解:
>>> li= ['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']
>>> [li[i:i+5] for i in range(0,len(li),5)]
[['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']]
Or, if you don't mind tuples, use zip:
或者,如果您不介意元组,请使用 zip:
>>> zip(*[iter(li)]*5)
[('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')]
Or apply list
to the tuples:
或应用于list
元组:
>>> map(list, zip(*[iter(li)]*5))
[['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']]