用户在python中输入的多维数组 [英] Multidimensional array by user input in python

查看:48
本文介绍了用户在python中输入的多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

错误:

<块引用>

回溯(最近一次调用最后一次)<ipython-input-64-4d8986a5e412>在<模块>5 for i in range(lengthrow):6 for j in range(lengthcol):---->7 arr[i][j]=int(input("输入值"))8 打印(arr)

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

代码:

from numpy import*arr = 数组([[],[]])lengthrow=int(input("输入数组行长度"))lengthcol=int(input("输入数组col长度"))对于我在范围内(lengthrow):对于范围内的 j(lengthcol):arr[i][j]=int(input("输入值"))打印(arr)

解决方案

我接受了@Austin 很好的回答并做了一些小改动:

将 numpy 导入为 npn_rows = int(input("请输入行数:"))n_cols = int(input("请输入列数:"))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))]打印(np.array(arr))

输出为:

输入行数:2输入列数:3输入 1. 行和 1. 列的值:1输入 1. 行和 2. 列的值:2输入 1. 行和 3. 列的值:3输入 2. 行和 1. 列的值:4输入 2. 行和 2. 列的值:5输入 2. 行和 3. 列的值:6[[1 2 3][4 5 6]]

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

<小时>

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

outer_arr = []对于范围内的 r(n_rows):内部_arr = []对于范围内的 c(n_cols):num = int(input("输入 {}.row 和 {}.column 的值:".format(r + 1, c + 1)))inner_arr.append(num)external_arr.append(inner_arr)打印(np.array(outer_arr))

I used Jupyter notebook, I am new to Python, I try to fetch value from user in multidimensional array how I do that? I write a little code, after put first value I get error that I don't understand

Error:

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: index 0 is out of bounds for axis 0 with size 0

code:

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)

解决方案

I took @Austin great answer and made some little changes:

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))

The output is:

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]]

You got an exception, because you initialized an empty array and used invalid indices. With this answer you generate the array after you have entered the users input.


Here is the long version of the one-liner (arr = [[...) which gives you the same result:

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))

这篇关于用户在python中输入的多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆