English 中文(简体)
p[复制]的双层阵列
原标题:Two dimensional array in python [duplicate]
  • 时间:2011-11-18 13:28:33
  •  标签:
  • python

我想知道如何在沙捞越两阵。

arr = [[]]

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

前两个派任的罚款。 但是,当我试图做到时,arr ***.append(“bb1”),我有以下错误:

IndexError: list index out of range.

在试图宣布2-D阵列时,我做了任何sil事?

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

最佳回答

你没有在座标上“申报”阵列或任何其他东西。 你们只是分配到(新的)变量。 如果你想要一个多层面的阵列,就只是增加一个阵列,作为阵容的组成部分。

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

arr = []
arr.append([ aa1 ,  aa2 ])
问题回答

有一些多层面阵列,如沙尔,你有一份包含其他清单的清单。

>>> arr = [[]]
>>> len(arr)
1

What you have done is declare a list containing a single list. So arr[0] contains a list but arr[1] is not defined.

您可以确定一份清单,列出以下两个清单:

arr = [[],[]]

Or to define a longer list you could use:

>>> arr = [[] for _ in range(5)]
>>> arr
[[], [], [], [], []]

页: 1 这是:

arr = [[]] * 3

在集装箱名单上的所有三个地点,如相同的名单:

>>> arr[0].append( test )
>>> arr
[[ test ], [ test ], [ test ]]

你在此再次使用的不是阵列,而是名单(名单)。

If you want multidimensional arrays in Python, you can use Numpy arrays. You d need to know the shape in advance.

例如:

 import numpy as np
 arr = np.empty((3, 2), dtype=object)
 arr[0, 1] =  abc 

You try to append to second element in array, but it does not exist. Create it.

arr = [[]]

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

We can create multidimensional array dynamically as follows,

Create 2 variables to read x and y from standard input:

 print("Enter the value of x: ")
 x=int(input())

 print("Enter the value of y: ")
 y=int(input())

1. 建立一个包含初始价值、0或任何使用以下代码的物品的清单

z=[[0 for row in range(0,x)] for col in range(0,y)]

• 为您的阵列数据创建行数和栏目。

标准投入的阅读数据:

for i in range(x):
         for j in range(y):
             z[i][j]=input()

显示结果:

for i in range(x):
         for j in range(y):
             print(z[i][j],end=   )
         print("
")

或以另一种方式显示在动态制造的阵列上,

for row in z:
     print(row)

在沙尔建造多维名单时 我通常使用类似于ThefMaster的解决方案,但不是将项目推到索引0,然后将项目编号为1,等等,我总是使用索引<代码>-1<<>>>>>>>/代码,自动成为该阵列最后项目的索引。

i.e.

arr = []

arr.append([])
arr[-1].append("aa1")
arr[-1].append("aa2")

arr.append([])
arr[-1].append("bb1")
arr[-1].append("bb2")
arr[-1].append("bb3")

页: 1

您可以首先向初始阵列提供元件,然后为了方便起见,您可以将其转换成一个绝热阵列。

import numpy as np
a = [] # declare null array
a.append([ aa1 ]) # append elements
a.append([ aa2 ])
a.append([ aa3 ])
print(a)
a_np = np.asarray(a) # convert to numpy array
print(a_np)
a = [[] for index in range(1, n)]

编辑方案

(1) 输入2D-Array值

row=input()
main_list=[]
for i in range(0,row):
    temp_list=map(int,raw_input().split(" "))
    main_list.append(temp_list)

2) For displaying 2D Array

for i in range(0,row):
    for j in range(0,len(main_list[0]):
        print main_list[i][j],
        print

以上方法对我来说并不可行,因为在这种情况下,我想根据情况将数据从2D阵列转移到一个新的阵列。 这种方法行之有效

a_2d_list = [[1, 2], [3, 4]]
a_2d_list.append([5, 6])
print(a_2d_list)

OUTPUT - [[1, 2], [3, 4], [5, 6]]
x=3#rows
y=3#columns
a=[]#create an empty list first
for i in range(x):
    a.append([0]*y)#And again append empty lists to original list
    for j in range(y):
         a[i][j]=input("Enter the value")

www.un.org/Depts/DGACM/index_spanish.htm 我不得不这样做:。

for index, user in enumerate(users):
    table_body.append([])
    table_body[index].append(user.user.id)
    table_body[index].append(user.user.username)

产出:

[[1,  john ], [2,  bill ]]




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签