如何在Python中添加相同数组中的不同元素?

时间:2021-01-13 13:43:37

I am a very new in Python so please forgive me the basic question.

我是Python的新手,所以请原谅我的基本问题。

I have an array with 400 float elements, and I need to add the first term with the second and divide by two.

我有一个包含400个浮动元素的数组,我需要将第一项与第二项相加,除以2。

I was trying something like:

我试过的是:

x1=[0,...,399]

n = len(x1)

x2 = []

i = 0
for i in range(0,n): 
    x2[i]=(x1[i]+x1[i+1])/2

But it gives me the error: IndexError: list assignment index out of range

但它给了我一个错误:IndexError:列表分配索引超出范围。

Thank you in advance.

提前谢谢你。

4 个解决方案

#1


3  

The problem here is that you cannot assign a value to an index in a list that is higher than the length of the list. Since you just want to keep adding items to the list, use the list.append() method instead:

这里的问题是,不能将值赋给列表中高于列表长度的索引。因为您只想一直向列表中添加项,所以请使用list.append()方法:

n = len(x1)

x2 = []

i = 0
for i in range(n-1): 
    x2.append((x1[i]+x1[i+1])/2)

Note that I also decreased the range by one, otherwise x1[i+1] will cause an IndexError.

注意,我还将范围缩小了1,否则x1[I +1]将导致索引错误。

#2


1  

A shorter and faster one-line solution using list comprehensions:

使用列表理解的更短、更快的一行解决方案:

x1=range(0,400)  #use xrange if on python 2.7
x2=[(x1[i]+x1[i+1])/2 for i in range(len(x1)) if i<len(x1)-1]

#3


1  

The most succinct way I can think of expressing this:

我能想到的最简洁的表达方式是:

[(i + j)/2 for i, j in zip(xrange(400), xrange(1,400))]

Or, equivalently:

或者,相当于:

xs = range(400)
[(i + j)/2 for i, j in zip(xs, xs[1:])]

Obviously, in Python3, xrange is obsolete, so there you could use range instead. Also, in Python3, the default behavior of / changes, so you'd have to use // instead if you want integers.

显然,在Python3中,xrange已经过时了,所以你可以用range代替。同样,在Python3中,默认的行为是/改变,所以如果你想要整数,你必须使用//。

#4


0  

FP-pythonic way:

FP-pythonic道:

x1 = [1.0, 2.0, 3.0, 4.0, 5.0]
x2 = map(lambda x, y: (x + y) / 2, x1, [0] + x1[:-1])

#1


3  

The problem here is that you cannot assign a value to an index in a list that is higher than the length of the list. Since you just want to keep adding items to the list, use the list.append() method instead:

这里的问题是,不能将值赋给列表中高于列表长度的索引。因为您只想一直向列表中添加项,所以请使用list.append()方法:

n = len(x1)

x2 = []

i = 0
for i in range(n-1): 
    x2.append((x1[i]+x1[i+1])/2)

Note that I also decreased the range by one, otherwise x1[i+1] will cause an IndexError.

注意,我还将范围缩小了1,否则x1[I +1]将导致索引错误。

#2


1  

A shorter and faster one-line solution using list comprehensions:

使用列表理解的更短、更快的一行解决方案:

x1=range(0,400)  #use xrange if on python 2.7
x2=[(x1[i]+x1[i+1])/2 for i in range(len(x1)) if i<len(x1)-1]

#3


1  

The most succinct way I can think of expressing this:

我能想到的最简洁的表达方式是:

[(i + j)/2 for i, j in zip(xrange(400), xrange(1,400))]

Or, equivalently:

或者,相当于:

xs = range(400)
[(i + j)/2 for i, j in zip(xs, xs[1:])]

Obviously, in Python3, xrange is obsolete, so there you could use range instead. Also, in Python3, the default behavior of / changes, so you'd have to use // instead if you want integers.

显然,在Python3中,xrange已经过时了,所以你可以用range代替。同样,在Python3中,默认的行为是/改变,所以如果你想要整数,你必须使用//。

#4


0  

FP-pythonic way:

FP-pythonic道:

x1 = [1.0, 2.0, 3.0, 4.0, 5.0]
x2 = map(lambda x, y: (x + y) / 2, x1, [0] + x1[:-1])