【问题标题】:How to construct a matrix based on user input?如何根据用户输入构建矩阵?
【发布时间】:2016-07-10 16:52:42
【问题描述】:

如何解决用随机数(用户输入)创建矩阵的问题? 我也有这个问题:

UnicodeDecodeError: 'ascii' 编解码器无法解码位置上的字节 0xc3 23: 序数不在范围内(128)

array = list()
i = int(input("How many row: "))      
j = int(input("How many column: "))    


for x in range(i):                
    if i <= 0 or i>10:
        print("Failure!")  
        break
    elif j <= 0 or j>10:
        print("Failure!")
        break
    else:
        for y in range(j):        
            num = raw_input("Value: ")       
            array.append(int(num))


a = np.reshape((i,j))
print a

【问题讨论】:

  • 这有什么随机性?

标签: python matrix input


【解决方案1】:

我将np.reshape((i,j)) 更改为a = np.reshape(array,(i,j)),它工作正常。我没有看到你提到的代码中有任何错误。

import numpy as np
array = list()
i = int(input("How many row: "))
j = int(input("How many column: "))


for x in range(i):
    if i <= 0 or i>10:
         print("Failure!")
         break
    elif j <= 0 or j>10:
         print("Failure!")
         break
    else:
        for y in range(j):
             num = raw_input("Value: ")
             array.append(int(num))


a = np.reshape(array,(i,j))
print a

【讨论】:

  • 非常感谢您! :D 很简单,但我几个小时都找不到它:)
猜你喜欢
  • 1970-01-01
  • 2020-12-11
  • 1970-01-01
  • 2020-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多