Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中使用列表列表的2D数组_Python_Arrays_Python 3.x_List - Fatal编程技术网

python中使用列表列表的2D数组

python中使用列表列表的2D数组,python,arrays,python-3.x,list,Python,Arrays,Python 3.x,List,我正在尝试创建一个用于输入和输出2D数组的函数。 经过一些研究(在使用1D数组时),我发现python中没有类似数组的东西。然而,我可以使用列表来实现我的目标 以下代码使用list为1D阵列工作: def array_input(num): for Index in range(0, num): ind = int(input("Please enter element {0} : ".format(Index))) array_list.append(i

我正在尝试创建一个用于输入和输出2D数组的函数。 经过一些研究(在使用1D数组时),我发现python中没有类似数组的东西。然而,我可以使用列表来实现我的目标

以下代码使用list为1D阵列工作:

def array_input(num):
    for Index in range(0, num):
        ind = int(input("Please enter element {0} : ".format(Index)))
        array_list.append(ind)


def array_output():
    for Index in range(0, len(array_list)):
            print("Element {0} is {1} ".format(Index, array_list[int(Index)]))
    """print(array_list)"""


array_list = []
a = int(input("Please enter the length of the array"))
array_input(a)
array_output()

input("Pres any key to continue")
以下是我使用列表列表为2D数组编写的内容:输出正常,但输入无效。有谁能帮我弄清楚如何向列表中添加新元素(有点像2D矩阵)


这应该对你有用。当然,您可以将
a
b
的分配切换到用户输入。重要的部分是数组_list变量的赋值。希望这能回答您的问题。

这将允许您在使用阵列时拥有最大的灵活性

def array_input(row, column):
    for R in range(0, row):
        for C in range(0, column):
            ind = int(input("Please enter element ({0},{1}) : ".format(R, C)))
            array_list[R][C] = ind
            print(ind)

def array_output(row, column):
    for R in range(0, row):
        for C in range(0, column):
            print("Element ({0},{1}) is {2} ".format(R, C, array_list[int(R)][int(C)]))
    print(array_list)

def create_array(row,column):
    array_list=[]
    for R in range(0,row):
        array_list.append([])
        for C in range(0,column):
            array_list[R].append(0)
    return array_list

a = int(input("Please enter the number of rows of the array "))
b = int(input("Please enter the number of columns of the array "))

array_list= create_array(a,b)
array_input(a,b)
array_output(a,b)

快乐编码

我发现python中没有数组之类的东西……哇,检查一下,代码的核心问题是它试图改变没有内容的位置;只有一行,不包含任何列。这两个答案解决了这个问题,首先创建一个在所有位置都包含0的矩阵。谢谢你,这真的很有帮助,我可以帮你!查看Yann Vernier对您的问题的评论,以了解为什么您的解决方案不起作用,因为我遗漏了该信息:)
def array_input(row, column):

    for R in range(0, row):
        for C in range(0, column):
            ind = int(input("Please enter element ({0},{1}) : ".format(R, C)))
            array_list[R][C] = ind
            print(ind)

def array_output(row, column):
    for R in range(0, row):
        for C in range(0, column):
            print("Element ({0},{1}) is {2} ".format(R, C, array_list[int(R)][int(C)]))
    print(array_list)


a, b = 2, 2;
array_list = [[0 for x in range(a)] for y in range(b)] 
array_input(2,2)
array_output(2,2)
def array_input(row, column):
    for R in range(0, row):
        for C in range(0, column):
            ind = int(input("Please enter element ({0},{1}) : ".format(R, C)))
            array_list[R][C] = ind
            print(ind)

def array_output(row, column):
    for R in range(0, row):
        for C in range(0, column):
            print("Element ({0},{1}) is {2} ".format(R, C, array_list[int(R)][int(C)]))
    print(array_list)

def create_array(row,column):
    array_list=[]
    for R in range(0,row):
        array_list.append([])
        for C in range(0,column):
            array_list[R].append(0)
    return array_list

a = int(input("Please enter the number of rows of the array "))
b = int(input("Please enter the number of columns of the array "))

array_list= create_array(a,b)
array_input(a,b)
array_output(a,b)