0

I am newbie in Python ... I am dealing with an array of 10 elements A[0,10] trying to do this loop:

for i in range(1, 9):
    C.append((A[i+1]-A[i-1])/2)

to find the average value of each point...

afterwards If I wish to print the results :

print(i, A[i], C[i])
print(i, A[i], C[i-2])

the second print is only working, the first is not.. please why this? And I need also to define the first and the last value of C[1] and C[10] and I can't do that because the first value is the C[-2] which is C[9] ...

if someone has any idea how to solve it....

4

3 回答 3

2

It's not usual to iterate over python lists (and I assume you mean a list) by index. You're operating on pairs of items, so a more idiomatic way to do this would be:

for prev, next in zip(a, a[2:]):
    # do whatever

Look up zip in the standard documentation. The two variables are assigned from the results of zip using sequence assignment, which is just where a comma-separated list of variables is bound to the elements of a sequence.

The second thing you're doing that is unidiomatic is to append to a list in a loop. This sort of thing is so common that python has a specific syntax for it:

[(next - prev)/2 for prev, next in zip(a, a[2:])]

This is called a list comprehension. If you can't guess what that does after pasting it into the interpreter, read about it in the standard documentation and the many articles available via google.

That code in action;

>>> import random
>>> a = range(5,15)
>>> random.shuffle(a)
>>> a
[11, 5, 12, 14, 6, 7, 8, 13, 10, 9]
>>> [(next - prev)/2 for prev, next in zip(a, a[2:])]
[0, 4, -3, -4, 1, 3, 1, -2]
>>>
于 2012-09-22T02:38:59.043 回答
1

您使用对称差异来查找C,但边缘点 (0,9) 只有一个邻居。您可以通过使用边缘点的左右差异来修复它:

n = len(A) # 10 in your case
C = [0.] * n

if n > 1:
   C[0] = (A[1] - A[0]) / 2.
   C[-1] = (A[-1] - A[-2]) / 2.

for i in xrange(1, n-1):
    C[i] = (A[i+1] - A[i-1]) / 2.

是否合适取决于您C以后希望如何使用该列表。

如果您需要C[0] = C[1],那么它甚至更简单:

n = len(A)
C = [0.] * n

for i in xrange(1, n-1):
    C[i] = (A[i+1] - A[i-1]) / 2.

if n > 1:
   C[0] = C[1]
   C[-1] = C[-2] # for symmetry

如果n == 0即A为空,则C为空。

如果n == 1即,A 正好有一个元素,那么 C 正好有一个元素:C[0] = 0.

于 2012-09-22T02:43:12.780 回答
0

Python 列表总是从 0 开始索引。如果你这样做

c = []         # c is a list
c.append(3)    # append 3 to the end of the list
print c        # => [3]
print c[0]     # => 3

因此,您的代码将 (A[0]+A[2])/2 放入 C[0],然后将 (A[1]+A[3])/2 放入 C[1],以此类推。

您可能希望首先将 C 创建为包含适当数量的 0 的列表,例如

c = [0.]*10    # create a 10-item list

# calculate smoothed values from a
for i in xrange(1,9):
    c[i] = 0.25*a[i-1] + 0.5*a[i] + 0.25*a[i+2]

# clean up the ends
c[0] = 0.75*a[0] + 0.25*a[1]
c[9] = 0.25*a[8] + 0.75*a[9]
于 2012-09-22T02:29:13.357 回答