Python数组:用数据填充数组的公式

时间:2015-09-18 03:47:52

标签: python arrays array-formulas

我希望使用公式中的数据填充24 x 4数组。 基于4个初始值,如[0, 0, 2137, 1419],数组应根据下面的输出表填充数字。

在Excel中,这很容易,不时使用。但是当经常使用并且在a,b,c或d中改变值时,让Python创建各种数组是最有帮助的。

问题:如何在Python中实现这一目标?

我假设嵌套for i in j loops可能会完成这项工作,但说实话,我迷失在这里。非常感谢帮助。

初始数据:

a+a-使用7行

b+b-使用5行

a = 0   b = 0   c = 2137  d = 1419

公式: 上半部分具有上升值,下半部分具有下降值。 当x + = 1,x = x,x = 1并且x = x的流在列之间移位时,存在非常逻辑的顺序。 重要说明:每个公式都是指其上方行中的前一个值。

a = 0   b = 0   c = 2137  d = 1419 

a+=1    b=b     c+=1      d=d           0
a+=1    b=b     c+=1      d=d           1
a+=1    b=b     c+=1      d=d           2
a+=1    b=b     c+=1      d=d           3
a+=1    b=b     c+=1      d=d           4
a+=1    b=b     c+=1      d=d           5
a+=1    b=b     c+=1      d=d           6 (7 for rows is known)
a=a     b+=1    c=c       d+=d        0
a=a     b+=1    c=c       d+=d        1
a=a     b+=1    c=c       d+=d        2
a=a     b+=1    c=c       d+=d        3
a=a     b+=1    c=c       d+=d        4   (5 for rows is known)
a-=a    b=b     c-=c      d=d           0
a-=a    b=b     c-=c      d=d           1
a-=a    b=b     c-=c      d=d           2
a-=a    b=b     c-=c      d=d           3
a-=a    b=b     c-=c      d=d           4
a-=a    b=b     c-=c      d=d           5
a-=a    b=b     c-=c      d=d           6 (7 for rows is known)
a=a     b-=b    c=c       d-=d        0
a=a     b-=b    c=c       d-=d        1
a=a     b-=b    c=c       d-=d        2
a=a     b-=b    c=c       d-=d        3
a=a     b-=b    c=c       d-=d        4   (5 for rows is known)
                                    Rows    
0       1       2         3         Columns

输出:

array = ([0,0,2137,1419],
[1,0,2138,1419],
[2,0,2139,1419],
[3,0,2140,1419],
[4,0,2141,1419],
[5,0,2142,1419],
[6,0,2143,1419],
[7,0,2144,1419],
[7,1,2144,1420],
[7,2,2144,1421],
[7,3,2144,1422],
[7,4,2144,1423],
[7,5,2144,1424],
[6,5,2143,1424],
[5,5,2142,1424],
[4,5,2141,1424],
[3,5,2140,1424],
[2,5,2139,1424],
[1,5,2138,1424],
[0,5,2137,1424],
[0,4,2137,1423],
[0,3,2137,1422],
[0,2,2137,1421],
[0,1,2137,1420],
[0,0,2137,1419])

1 个答案:

答案 0 :(得分:1)

您尚未回复我的评论。但是看看 公式: 之后的所需输出和文本,我认为你真的想加/减1而不是变量本身。

因此,您基本上会在前7行中重复添加向量[1,0,1,0],然后在接下来的5行中添加[0,1,0,1],然后再次减去相同的内容。
这很好地是线性的,因此您可以累加它们并将结果始终应用到第一行。这对numpy非常有用!

import numpy as np
import itertools as it

# first 7 rows add 1 to a and 1 to c
add1 = np.array([1, 0, 1, 0])

# next 5 rows add 1 to b and 1 to d
add2 = np.array([0, 1, 0, 1])

# stack them accordingly
upper = np.vstack(list(it.chain(it.repeat(add1, 7),
                                it.repeat(add2, 5))))

# lower is the negated version of upper
lower = -upper

# stack them
both = np.vstack((upper,
                  lower))

# with cumsum we'll get for each row the relative distance to the first row
# (istead of distance to previous)
sums = np.cumsum(both, axis=0)

# prepend 0 vector to retain the the first row
sums = np.vstack((np.zeros_like(add1), sums))

# create the frist row
l = np.array([0, 0, 2137, 1419])

# now just add up row and sums
result = l+sums

print(result)

这对于大型阵列来说非常快。但是,如果您没有numpy或者不想安装它,可以使用一些zipmap技巧来实现等效方法。

import itertools as it

def addVecs(a, b):
    return [e1 + e2 for e1, e2 in zip(a, b)]


def scaleVec(a, s):
    return [e*s for e in a]


# first 7 rows add 1 to a and 1 to c
add1 = [1, 0, 1, 0]

# next 5 rows add 1 to b and 1 to d
add2 = [0, 1, 0, 1]

# stack them accordingly
upper = list(it.chain(it.repeat(add1, 7),
                      it.repeat(add2, 5)))

# lower is the negated version of upper
lower = list(it.starmap(scaleVec, zip(upper, it.repeat(-1))))

# stack them
both = upper + lower

# create cumsum to get for each row the relative distance to the first row
# (istead of distance to previous)
sums = [[0, 0, 0, 0]]
for row in both:
    sums.append(addVecs(sums[-1], row))

# the first row
l = [0, 0, 2137, 1419]

# now for each row in sums, add it to l
result2 = list(it.starmap(addVecs, zip(it.repeat(l), sums)))
for row in result2:
    print(row)

两个结果都包含您想要的输出:

[[   0    0 2137 1419]
 [   1    0 2138 1419]
 [   2    0 2139 1419]
 [   3    0 2140 1419]
 [   4    0 2141 1419]
 [   5    0 2142 1419]
 [   6    0 2143 1419]
 [   7    0 2144 1419]
 [   7    1 2144 1420]
 [   7    2 2144 1421]
 [   7    3 2144 1422]
 [   7    4 2144 1423]
 [   7    5 2144 1424]
 [   6    5 2143 1424]
 [   5    5 2142 1424]
 [   4    5 2141 1424]
 [   3    5 2140 1424]
 [   2    5 2139 1424]
 [   1    5 2138 1424]
 [   0    5 2137 1424]
 [   0    4 2137 1423]
 [   0    3 2137 1422]
 [   0    2 2137 1421]
 [   0    1 2137 1420]
 [   0    0 2137 1419]]

我在笔记本电脑上测试了这两种方法的性能。 sums已经建立numpy,需要6.29μs,普通python需要29.5μs。