简体   繁体   English

在 Python 中如何声明动态数组

[英]In Python How can I declare a Dynamic Array

I want to declare an Array and all items present in the ListBox Should Be deleted irrespective of the Group name present in the ListBox.我想声明一个数组,并且无论列表框中存在的组名称如何,都应删除列表框中存在的所有项目。 can any body help me coding in Python.任何人都可以帮我用 Python 编码吗? I am using WINXP OS & Python 2.6.我使用的是 WINXP 操作系统和 Python 2.6。

In Python, a list is a dynamic array.在 Python 中, list是一个动态数组。 You can create one like this:你可以像这样创建一个:

lst = [] # Declares an empty list named lst

Or you can fill it with items:或者你可以用项目填充它:

lst = [1,2,3]

You can add items using "append":您可以使用“追加”添加项目:

lst.append('a')

You can iterate over elements of the list using the for loop:您可以使用for循环遍历列表的元素:

for item in lst:
    # Do something with item

Or, if you'd like to keep track of the current index:或者,如果您想跟踪当前索引:

for idx, item in enumerate(lst):
    # idx is the current idx, while item is lst[idx]

To remove elements, you can use the del command or the remove function as in:要删除元素,您可以使用 del 命令或 remove 函数,如下所示:

del lst[0] # Deletes the first item
lst.remove(x) # Removes the first occurence of x in the list

Note, though, that one cannot iterate over the list and modify it at the same time;但是请注意,不能同时遍历列表并修改它; to do that, you should instead iterate over a slice of the list (which is basically a copy of the list).为此,您应该迭代列表的一部分(基本上是列表的副本)。 As in:如:

 for item in lst[:]: # Notice the [:] which makes a slice
       # Now we can modify lst, since we are iterating over a copy of it

In python, A dynamic array is an 'array' from the array module.在python中,动态数组是来自数组模块的“数组”。 Eg例如

from array import array
x = array('d')          #'d' denotes an array of type double
x.append(1.1)
x.append(2.2)
x.pop()                 # returns 2.2

This datatype is essentially a cross between the built-in 'list' type and the numpy 'ndarray' type.这种数据类型本质上是内置“列表”类型和 numpy“ndarray”类型之间的交叉。 Like an ndarray, elements in arrays are C types, specified at initialization.与 ndarray 一样,数组中的元素是 C 类型,在初始化时指定。 They are not pointers to python objects;它们不是指向 python 对象的指针; this may help avoid some misuse and semantic errors, and modestly improves performance.这可能有助于避免一些误用和语义错误,并适度提高性能。

However, this datatype has essentially the same methods as a python list, barring a few string & file conversion methods.但是,除了一些字符串和文件转换方法之外,此数据类型具有与 Python 列表基本相同的方法。 It lacks all the extra numerical functionality of an ndarray.它缺少 ndarray 的所有额外数字功能。

See https://docs.python.org/2/library/array.html for details.有关详细信息,请参阅https://docs.python.org/2/library/array.html

Here's a great method I recently found on a different stack overflow post regarding multi-dimensional arrays, but the answer works beautifully for single dimensional arrays as well:这是我最近在有关多维数组的不同堆栈溢出帖子中发现的一个很好的方法,但该答案也适用于一维数组:

# Create an 8 x 5 matrix of 0's:
w, h = 8, 5;
MyMatrix = [ [0 for x in range( w )] for y in range( h ) ]  

# Create an array of objects:
MyList = [ {} for x in range( n ) ]

I love this because you can specify the contents and size dynamically, in one line!我喜欢这个,因为您可以在一行中动态指定内容和大小!

One more for the road:再来一张公路:

# Dynamic content initialization:
MyFunkyArray = [ x * a + b for x in range ( n ) ]

you can declare a Numpy array dynamically for 1 dimension as shown below:您可以为 1 维动态声明一个 Numpy 数组,如下所示:

import numpy as np将 numpy 导入为 np

n = 2
new_table = np.empty(shape=[n,1])

new_table[0,0] = 2
new_table[1,0] = 3
print(new_table)

The above example assumes we know we need to have 1 column but we want to allocate the number of rows dynamically (in this case the number or rows required is equal to 2)上面的例子假设我们知道我们需要有 1 列,但我们想动态分配行数(在这种情况下,所需的行数等于 2)

output is shown below:输出如下所示:

[[2.] [3.]] [[2.] [3.]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM