【问题标题】:Multidimensional array by user input in pythonpython中用户输入的多维数组
【发布时间】:2019-02-12 15:55:59
【问题描述】:

我使用 Jupyter 笔记本,我是 Python 新手,我尝试从多维数组中的用户获取值,我该怎么做?我写了一点代码,在输入第一个值后,我得到了我不理解的错误

错误:

Traceback (most recent call last)
   <ipython-input-64-4d8986a5e412> in <module>
          5 for i in range(lengthrow):
          6     for j in range(lengthcol):
    ----> 7         arr[i][j]=int(input("enter value"))
          8 print(arr)

IndexError:索引 0 超出轴 0 的范围,大小为 0

代码:

from numpy import*
arr = array([[],[]])
lengthrow=int(input("enter array row length"))
lengthcol=int(input("enter array col length"))
for i in range(lengthrow):
    for j in range(lengthcol):
        arr[i][j]=int(input("enter value"))
print(arr)

【问题讨论】:

  • 你创建了 arr 一个 (2,0) 形状数组。 arr[0] 是一个 (0,) 形状数组。尝试使用 [0] 对其进行索引会产生错误。

标签: python numpy multidimensional-array


【解决方案1】:

我采纳了@Austin 很好的答案并做了一些小改动:

import numpy as np

n_rows = int(input("Enter number of rows: "))
n_cols = int(input("Enter number of columns: "))

arr = [[int(input("Enter value for {}. row and {}. column: ".format(r + 1, c + 1))) for c in range(n_cols)] for r in range(n_rows)]

print(np.array(arr))

输出是:

Enter number of rows: 2
Enter number of columns: 3
Enter value for 1. row and 1. column: 1
Enter value for 1. row and 2. column: 2
Enter value for 1. row and 3. column: 3
Enter value for 2. row and 1. column: 4
Enter value for 2. row and 2. column: 5
Enter value for 2. row and 3. column: 6
[[1 2 3]
 [4 5 6]]

您遇到了异常,因为您初始化了一个空数组并使用了无效索引。有了这个答案,您可以在输入用户输入后生成数组。


这里是单行 (arr = [[...) 的长版本,它给你同样的结果:

outer_arr = []
for r in range(n_rows):
    inner_arr = []
    for c in range(n_cols):
        num = int(input("Enter value for {}. row and {}. column: ".format(r + 1, c + 1)))
        inner_arr.append(num)
    outer_arr.append(inner_arr)

print(np.array(outer_arr))

【讨论】:

  • 谢谢Darius Morawec,这个方法也很棒实际上我需要这个输出非常感谢。你能推荐一些学习二维数组的参考吗?我需要深入了解它。谢谢:)跨度>
  • 当然,多维数组是线性代数中的矩阵,它可以让你进行强大的运算。这里有一些介绍:#1#2#3#4
  • 再次感谢@Darius Morawiec :)
  • 你能解释一下这一行 [[int(input("Enter value for {}.row and {}.column: ".format(r + 1, c + 1))) for c in range(n_cols)] for r in range(n_rows)] 我不太明白 for 循环的流程
  • @ToufikKhan 当然,我编辑了答案并添加了相同代码的更长版本。我们使用两个 for 循环来创建一个列表列表,我们将其转换为二维数组。
【解决方案2】:

您可以在此处使用列表理解,因为最终结果是列表列表:

lengthrow = int(input("enter array row length: "))
lengthcol = int(input("enter array col length: "))

arr = [[int(input("enter value: ")) for _ in range(lengthcol)] for _ in range(lengthrow)]

print(arr)

您的代码有问题

arr = array([[],[]])
print(arr.shape)
# (2, 0)

这意味着您在数组中有一个大小为 0 的列,因此当您执行 arr[0][0] 时,例如,它会引发错误。

【讨论】:

    【解决方案3】:

    问题在于初始数组的形状:

    In [1]: arr = np.array([[], []])
    In [2]: arr
    Out[2]: array([], shape=(2, 0), dtype=float64)
    In [3]: arr[0]
    Out[3]: array([], dtype=float64)
    In [4]: arr[0][0]
    ... 
    IndexError: index 0 is out of bounds for axis 0 with size 0
    

    将此数组视为 2 行 0 列。您不能引用不存在的列。而且,与其他一些语言相比,您不能简单地通过引用新索引来增长数组。一旦创建,大小就固定了。

    虽然其他人展示了不错的列表理解方法,但我将展示如何正确使用您的方法:

    In [8]: lengthrow, lengthcol = 2,3    # or user input
    In [9]: arr = np.zeros((lengthrow, lengthcol), dtype=int)  # large enough array
    In [10]: arr
    Out[10]: 
    array([[0, 0, 0],
           [0, 0, 0]])
    In [11]: for i in range(2):
        ...:     for j in range(3):
        ...:         arr[i,j] = int(input(f'{i},{j}:'))
        ...:         
    0,0:0
    0,1:1
    0,2:2
    1,0:3
    1,1:4
    1,2:5
    In [12]: arr
    Out[12]: 
    array([[0, 1, 2],
           [3, 4, 5]])
    

    【讨论】:

    • 感谢您的宝贵重播,这个称呼真好。再次感谢你:)
    • np.array() 函数将列表转换为 numpy 数组
    猜你喜欢
    • 1970-01-01
    • 2018-08-05
    • 1970-01-01
    • 1970-01-01
    • 2021-05-21
    • 1970-01-01
    • 2021-05-11
    • 2013-03-30
    • 2021-12-07
    相关资源
    最近更新 更多