Python 如何创建 3x3 矩阵?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28205805/
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
How do I create 3x3 matrices?
提问by pk3
I have a 2D list containing these values:
我有一个包含这些值的二维列表:
text = [[4, 3, 8, 9, 5, 1, 2, 7, 6], [8, 3, 4, 1, 5, 9, 6, 7, 2],
[6, 1, 8, 7, 5, 3, 2, 9, 4], [6, 9, 8, 7, 5, 3, 2, 1, 4],
[6, 1, 8, 7, 5, 3, 2, 1, 4], [6, 1, 3, 2, 9, 4, 8, 7, 5]]
For instance, text[i] should be printed like this:
例如, text[i] 应该像这样打印:
4 3 8
9 5 1
2 7 6
But my matrix prints this:
但是我的矩阵打印了这个:
r = 6
m = []
for i in range(r):
m.append([int(x) for x in text[i]])
for i in m:
print (i)
>>
4 3 8 9 5 1 2 7 6
8 3 4 1 5 9 6 7 2
6 1 8 7 5 3 2 9 4
6 9 8 7 5 3 2 1 4
6 1 8 7 5 3 2 1 4
6 1 3 2 9 4 8 7 5
回答by Alex Martelli
def print9_as_3x3(list9):
for i in 0, 3, 6:
for j in 0, 1, 2:
print(list9[i+j], end= ' ')
print()
This can of course be easily generalized (using range
s instead of those literal tuples) but I'm trying to keep it as simple as feasible.
这当然可以很容易地概括(使用range
s 而不是那些文字元组),但我试图让它尽可能简单。
The Q's subject talks about creatingmatrices, which this code doesn't do, but the Q's body's all about printinginstead, so I've focused on that! Please clarify (edit your Q) and comment if you want something different...
Q 的主题谈到创建矩阵,这段代码没有做,但 Q 的主体完全是关于打印的,所以我专注于这一点!如果您想要不同的东西,请澄清(编辑您的 Q)并发表评论...
回答by Adam Smith
Use the grouper recipe referred to hereto split each list into 3's, then print each set of three.
使用这里提到的石斑鱼配方将每个列表分成 3 个,然后打印每组三个。
def grouper(iterable, n, fillvalue=None):
args = [iter(iterable)] * n
return itertools.zip_longest(*args, fillvalue=fillvalue)
# zip_longest is new in Python3
for matrix in text:
for chunk in grouper(matrix, 3):
print(chunk)
print() # empty line in between each 3x3
RESULT:
结果:
(4, 3, 8)
(9, 5, 1)
(2, 7, 6)
(8, 3, 4)
(1, 5, 9)
(6, 7, 2)
(6, 1, 8)
(7, 5, 3)
(2, 9, 4)
(6, 9, 8)
(7, 5, 3)
(2, 1, 4)
(6, 1, 8)
(7, 5, 3)
(2, 1, 4)
To make the literal output from the question, you'll have to do a bit of formatting but it shouldn't be too bad. Something like:
要从问题中生成文字输出,您必须进行一些格式化,但应该不会太糟糕。就像是:
for matrix in text:
for chunk in grouper(matrix, 3):
print(" ".join(map(str, chunk)))
print()
RESULT:
结果:
4 3 8
9 5 1
2 7 6
8 3 4
1 5 9
6 7 2
6 1 8
7 5 3
2 9 4
6 9 8
7 5 3
2 1 4
6 1 8
7 5 3
2 1 4
6 1 3
2 9 4
8 7 5
回答by Padraic Cunningham
for a, b ,c in (x[i:i+3] for x in text for i in range(0,len(x),3)):
print(a,b,c)
4 3 8
9 5 1
2 7 6
8 3 4
1 5 9
6 7 2
6 1 8
7 5 3
2 9 4
6 9 8
7 5 3
2 1 4
6 1 8
7 5 3
2 1 4
6 1 3
2 9 4
8 7 5
回答by dragon2fly
You can use numpy. First, convert your list into numpy array. Then, take an element and reshape it to 3x3 matrix.
您可以使用numpy。首先,将您的列表转换为 numpy 数组。然后,取一个元素并将其重塑为 3x3 矩阵。
import numpy as np
text = [[4, 3, 8, 9, 5, 1, 2, 7, 6], [8, 3, 4, 1, 5, 9, 6, 7, 2],
[6, 1, 8, 7, 5, 3, 2, 9, 4], [6, 9, 8, 7, 5, 3, 2, 1, 4],
[6, 1, 8, 7, 5, 3, 2, 1, 4], [6, 1, 3, 2, 9, 4, 8, 7, 5]]
text = np.array(text)
print text[0].reshape((3, 3))
print text[1].reshape((3, 3))
Output:
输出:
[[4 3 8]
[9 5 1]
[2 7 6]]
[[8 3 4]
[1 5 9]
[6 7 2]]
With numpy, you are really working with matrices
使用 numpy,您实际上是在处理矩阵
回答by martineau
It's not completely clear to me what you're after, so here's one guess:
我不完全清楚你在追求什么,所以这里有一个猜测:
text = [[4, 3, 8, 9, 5, 1, 2, 7, 6], [8, 3, 4, 1, 5, 9, 6, 7, 2],
[6, 1, 8, 7, 5, 3, 2, 9, 4], [6, 9, 8, 7, 5, 3, 2, 1, 4],
[6, 1, 8, 7, 5, 3, 2, 1, 4], [6, 1, 3, 2, 9, 4, 8, 7, 5]]
# convert to 3D list in-place
for i, data in enumerate(text):
text[i] = [[data[0], data[1], data[2]],
[data[3], data[4], data[5]],
[data[6], data[7], data[8]]]
# utility for print a 3x3 matrix
def formatrix(label, matrix):
indent = len(label)
return '\n'.join((label + ', '.join(map(str, matrix[0])),
indent*' ' + ', '.join(map(str, matrix[1])),
indent*' ' + ', '.join(map(str, matrix[2]))))
# print each matrix in reformatted list
for i, matrix in enumerate(text):
print(formatrix('text[%s]: ' % i, matrix))
Output:
输出:
text[0]: 4, 3, 8
9, 5, 1
2, 7, 6
text[1]: 8, 3, 4
1, 5, 9
6, 7, 2
text[2]: 6, 1, 8
7, 5, 3
2, 9, 4
text[3]: 6, 9, 8
7, 5, 3
2, 1, 4
text[4]: 6, 1, 8
7, 5, 3
2, 1, 4
text[5]: 6, 1, 3
2, 9, 4
8, 7, 5