如何在不使用追加的情况下将元素插入数组,Python?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17137184/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to insert elements into array without using append, Python?
提问by Tudor Hofnar
I'm a beginner with Python and am having a bit of trouble inserting elements into an array without using the append() function.
我是 Python 的初学者,在不使用 append() 函数的情况下将元素插入数组时遇到了一些麻烦。
This is part of my code which I hope illustrates enough but feel free to ask for more details if it helps:
这是我的代码的一部分,我希望它足够说明,但如果有帮助,请随时询问更多详细信息:
#other code
arr1 = []
arr2 = []
index1 = 0
index2 = 0
for i in range(0, len(A)):
if A[i] < A[r]:
arr1[index1] = A[i]
index1 = index1 + 1
elif A[i] > A[r]:
arr2[index2] = A[i]
index2 = index2 + 1
#other code
A is declared above this code and the number of elements in it vary based on an input file for the program. Currently I'm getting the index out of range error and on the assignment of A[i] to arr1[index1]. Any ideas? I just can't seem to get this working in Python.
A 在此代码上方声明,其中的元素数量因程序的输入文件而异。目前,我正在获取索引超出范围错误以及将 A[i] 分配给 arr1[index1]。有任何想法吗?我似乎无法在 Python 中使用它。
Thanks!
谢谢!
采纳答案by Ashwini Chaudhary
You can do that using +or +=operators:
您可以使用+或+=运算符来做到这一点:
>>> lis = []
>>> lis = lis + [1]
>>> lis
[1]
>>> lis = lis + [2]
>>> lis
[1, 2]
>>> lis += [3] # += acts like list.extend, i.e changes the list in-place
>>> lis
[1, 2, 3]
The problem with your code is that the lists arr1and arr2are empty, so
assigning values to indexes which don't exist yet is going to raise IndexError.
您的代码的问题在于列表arr1和arr2是空的,因此将值分配给尚不存在的索引将引发IndexError.
for i in range(0, len(A)):
if A[i] < A[r]:
arr1 = arr1 + [A[i]]
elif A[i] > A[r]:
arr2 = arr2 + [A[i]]
回答by David Marek
It looks like you are trying to implement something similar to quicksort. Lists in python are in reality growing arrays. New list is empty, so you can't insert a value into it by using an index. Using appendis the best option here, e.g.:
看起来您正在尝试实现类似于快速排序的东西。python 中的列表实际上是不断增长的数组。新列表为空,因此您无法使用索引向其中插入值。使用append是这里的最佳选择,例如:
a = [1, 5, 3, 2, 6, 7]
al = []
ag = []
for x in a:
if x < 4:
al.append(x)
else:
ag.append(x)
Now al == [1, 3, 2]and ag == [5, 6, 7].
现在al == [1, 3, 2]和ag == [5, 6, 7]。
If you already have an existing list, then you can access its elements by using an index. Another example where I have created the lists beforehand:
如果您已经有一个现有列表,则可以使用索引访问其元素。我预先创建列表的另一个示例:
a = [1, 5, 3, 2, 6, 7]
al = 3 * [0]
ag = 3 * [0]
index_l = 0
index_r = 0
for i in range(len(a)):
if a[i] < 4:
al[index_l] = a[i]
index_l += 1
else:
ag[index_r] = a[i]
index_r += 1
I don't think this is very pythonic, and you have to know how large you lists must be. Please don't use this approach.
我不认为这是非常pythonic,你必须知道你的列表必须有多大。请不要使用这种方法。
Also, it's not a good idea to use al += [a[i]], it's doing the same as append, but you are creating intermediate lists, so it's slower:
此外,使用 不是一个好主意al += [a[i]],它的作用与 append 相同,但您正在创建中间列表,因此速度较慢:
>>> timeit.timeit('a += [1]', 'a = [1,2,3]')
0.14568603380625794
>>> timeit.timeit('a.append(1)', 'a = [1,2,3]')
0.07830060367457214
Example of a simple quicksort:
一个简单的快速排序示例:
def qsort(data):
if len(data) <= 1:
return data
pivot = data[0]
smaller = []
greater = []
for x in data[1:]:
if x < pivot:
smaller.append(x)
else:
greater.append(x)
return qsort(smaller) + [pivot] + qsort(greater)

