Python中的二维数组 [英] Two dimensional array in python

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

问题描述

我想知道如何在Python中声明一个二维数组.

I want to know how to declare a two dimensional array in Python.

arr = [[]]

arr[0].append("aa1")
arr[0].append("aa2")
arr[1].append("bb1")
arr[1].append("bb2")
arr[1].append("bb3")

前两个任务工作正常.但是当我尝试执行arr[1].append("bb1")时,出现以下错误:

The first two assignments work fine. But when I try to do, arr[1].append("bb1"), I get the following error:

IndexError: list index out of range.

我在尝试声明二维数组时做任何愚蠢的事情吗?

Am I doing anything silly in trying to declare the 2-D array?

修改:
但是我不知道数组中元素的数量(行和列).


but I do not know the number of elements in the array (both rows and columns).

推荐答案

您不要在Python中声明"数组或其他任何东西.您只需分配一个(新)变量.如果要使用多维数组,只需添加一个新数组作为数组元素即可.

You do not "declare" arrays or anything else in python. You simply assign to a (new) variable. If you want a multidimensional array, simply add a new array as an array element.

arr = []
arr.append([])
arr[0].append('aa1')
arr[0].append('aa2')

arr = []
arr.append(['aa1', 'aa2'])

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

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