首页 > 解决方案 > python3.9.6 numpy.matrix() 出错

问题描述

import numpy as np


def clean_input(row:list):
    for i in range(len(row)):
        if row[i] == " ":
            row[i] = "0"
    if len(row) != 9:
        for i in range(9-len(row)):
            row.append("0")
    return row


# row1 = input(f"{1}th Row: ")
# row2 = input(f"{2}th Row: ")
# row3 = input(f"{3}rd Row: ")
# row4 = input(f"{4}th Row: ")
# row5 = input(f"{5}th Row: ")
# row6 = input(f"{6}th Row: ")
# row7 = input(f"{7}th Row: ")
# row8 = input(f"{8}th Row: ")
# row9 = input(f"{9}th Row: ")

row1 = "34 53  3"
row2 = "  4 546 "
row3 = ""
row4 = " "
row5 = "5"
row6 = "      "
row7 = "123456789"
row8 = "000000000"
row9 = " 34 76657"


row_list = [
    list(row1), list(row2), list(row3), list(row4), list(row5),
    list(row6), list(row7), list(row8), list(row9)
]

print(np.matrix(row_list)) #line 40

clear_list = []
for element in row_list:
    clear_list.append(clean_input(element))
print(np.matrix(row_list)) #line 45
 

(蟒蛇3.9.6)

如果你运行上面的代码,你会得到一个numpy错误并再次运行相同的代码,只需将print语句(第 40 行print(np.matrix(row_list)))替换到第 45 行或在for语句(第 43 行)之后,一切正常。

我确定第for43 行的声明不会影响第row_list35 行。如果我错了,请纠正我,很高兴学习。

我不知道它的Python问题或任何相关numpy

错误信息:

第 40 行的ifprint(np.matrix(row_list))语句

chitti@Thor /m/4/F/L/Py_files> python3 sudoku.py
/home/chitti/.local/lib/python3.9/site-packages/numpy/matrixlib/defmatrix.py:145:
VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple
of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, 
you must specify 'dtype=object' when creating the ndarray.
arr = N.array(data, dtype=dtype, copy=copy)
[[list(['3', '4', ' ', '5', '3', ' ', ' ', '3'])
  list([' ', ' ', '4', ' ', '5', '4', '6', ' ']) 
  list([]) 
  list([' '])
  list(['5'])
  list([' ', ' ', ' ', ' ', ' ', ' '])
  list(['1', '2', '3', '4', '5', '6', '7', '8', '9'])
  list(['0', '0', '0', '0', '0', '0', '0', '0', '0'])
  list([' ', '3', '4', ' ', '7', '6', '6', '5', '7'])]]

如果print(np.matrix(row_list))第 45 行或语句之后的for语句一切正常

chitti@Thor /m/4/F/L/Py_files> python3 sudoku.py
[['3' '4' '0' '5' '3' '0' '0' '3' '0']
 ['0' '0' '4' '0' '5' '4' '6' '0' '0']
 ['0' '0' '0' '0' '0' '0' '0' '0' '0']
 ['0' '0' '0' '0' '0' '0' '0' '0' '0']
 ['5' '0' '0' '0' '0' '0' '0' '0' '0']
 ['0' '0' '0' '0' '0' '0' '0' '0' '0']
 ['1' '2' '3' '4' '5' '6' '7' '8' '9']
 ['0' '0' '0' '0' '0' '0' '0' '0' '0']
 ['0' '3' '4' '0' '7' '6' '6' '5' '7']]

相片

图 1print(np.matrix(row_list))第 40 行的声明

图 2print(np.matrix(row_list))第 45 行的声明

标签: pythonnumpy

解决方案


正如@hpaulj 提到的原因,在您的代码中,row与element相同。row_list您的函数通过使用修改行,这些行确实是 的元素row[i] = ...row[i] = ...将更改导入的 arg。元素正在通过修改它们的函数。您的代码正在按以下示例进行处理:

import numpy as np
row_list = [[1, 2, 3], [3, 5, 4], [7, 8, 9]]
clear_list = []
for element in row_list:
    element[1] = 0
    clear_list.append(element)
print(np.matrix(row_list))

这将导致[[1 0 3] [3 0 4] [7 0 9]]. 很明显,元素被修改了。防止row_list修改的一种方法是复制它;一些代码如:

import copy
r_l = copy.deepcopy(row_list)
for element in r_l:
    clear_list.append(clean_input(element))

推荐阅读