打印 2x2 矩阵
Posted
技术标签:
【中文标题】打印 2x2 矩阵【英文标题】:Printing 2x2 matrix 【发布时间】:2018-03-03 02:59:40 【问题描述】:我编写了一个程序来打印一个 2x2 矩阵,其中每个矩阵中表示的数字是通过用户输入给出的。
例如:
userin = 1 2 #two digit input with a spacing in between
userin2 = 3 4 #userin and userin2 is first matrix
userin3 = 5 6
userin4 = 7 8 #userin3 and userin4 is second matrix
The program would then print out: [[1,2],[3,4]], #first matrix
[[5,6],[7,8]] #second matrix
我让程序按我的意愿工作,但我觉得我编写的代码效率太低,以至于我创建了太多空列表来满足用户输入。
userin = input("Enter first two digit for first 2x2 matrix: ").split(' ')
userin2 = input("Enter last two digit for first 2x2 matrix: ").split(' ')
userin3 = input("Enter first two digit for second 2x2 matrix: ").split(' ')
userin4 = input("Enter last two digit for second 2x2 matrix: ").split(' ')
lst1 = []
lst2 = []
lst3 = []
lst4 = []
matrix = []
matrix2 = []
for x in userin:
x = int(x)
lst1.append(x)
for j in userin2:
j = int(j)
lst2.append(j)
for x in userin3:
x = int(x)
lst3.append(x)
for j in userin4:
j = int(j)
lst4.append(j)
matrix.append(lst1)
matrix.append(lst2)
matrix2.append(lst3)
matrix2.append(lst4)
print(matrix)
print(matrix2)
程序以我想要的格式输出 [[1,2],[3,4]],[[5,6],[7,8]] 但我'我正在寻找一种更有效的方法。
请注意,不建议我使用 numpy 和其他内置库。
感谢您的帮助。
【问题讨论】:
这个问题更适合代码审查:codereview.stackexchange.com 查找列表理解并注意 string.split 返回一个列表 【参考方案1】:以下是一些替代方法的示例。我不确定第一个示例是否是您正在寻找的,但我把它扔进去是为了好玩!
>>> userin = '1 2'.split()
... userin2 = '3 4'.split()
... userin3 = '5 6'.split()
... userin4 = '7 8'.split()
>>> print('[[, ], [, ]]'.format(*userin, *userin2))
... print('[[, ], [, ]]'.format(*userin3, *userin4))
[[1, 2], [3, 4]]
[[5, 6], [7, 8]]
>>> print([[int(a) for a in pair] for pair in (userin, userin2)])
... print([[int(a) for a in pair] for pair in (userin3, userin4)])
[[1, 2], [3, 4]]
[[5, 6], [7, 8]]
【讨论】:
【参考方案2】:我会这样做:
# make a list of lists with the input
userin = [[input("Enter first two digit for first 2x2 matrix: ").strip().split(' '),
input("Enter last two digit for first 2x2 matrix: ").strip().split(' ')],
[input("Enter first two digit for second 2x2 matrix: ").strip().split(' '),
input("Enter last two digit for second 2x2 matrix: ").strip().split(' ')]]
for matrix in userin: # loop through inner lists
for index, row in enumerate(matrix): # loop through each matrix
matrix[index] = [int(item) for item in row] # convert input to integers
print(",\n".join(str(matrix) for matrix in userin)) # print
如果它总是只有这 4 个输入语句,我认为这很好。如果输入是可变的,那么做一个函数会更有意义。
输出:
Enter first two digit for first 2x2 matrix: 1 2
Enter last two digit for first 2x2 matrix: 3 4
Enter first two digit for second 2x2 matrix: 5 6
Enter last two digit for second 2x2 matrix: 7 8
[[1, 2], [3, 4]],
[[5, 6], [7, 8]]
【讨论】:
为什么不让ValueError
自己提高呢? ValueError: invalid literal for int() with base 10: 'a'
ValueError: invalid literal for int() with base 10: 'a'
,您的自定义消息有什么比这更清晰的吗?【参考方案3】:
为什么不放入 while 循环来获取任意数量的矩阵,为什么不一次获取所有 4 个元素?
类似:
user_in = ['0']
matrices = []
while (user_in[0].lower() != 'q'):
user_in = input("Input 2x2 matrix(space separated), enter 'q' to quit: ").split(' ')
if len(user_in) == 4:
a, b, c, d = user_in
m2x2 = [[int(a), int(b)], [int(c), int(d)]]
matrices.append(m2x2)
for matrix in matrices:
print(matrix)
【讨论】:
When a string contains single or double quote characters, however, use the other one to avoid backslashes in the string. It improves readability.以上是关于打印 2x2 矩阵的主要内容,如果未能解决你的问题,请参考以下文章