Python 数组:用数据填充数组的公式
Python array: formulas to populate array with data
我想用公式中的数据填充一个 24 x 4 数组。
基于 4 个初始值,如 [0, 0, 2137, 1419]
,数组应根据下面的输出 table 填充数字。
在 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])
你还没有回复我的评论。但是查看所需的输出和 Formulas: 之后的文本我想,你真的是想 add/subtract 1 而不是变量他们自己。
所以,您基本上是在前 7 行中重复添加向量 [1,0,1,0]
,然后在接下来的五行中添加 [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 或不想安装它,则可以使用一些 zip
和 map
技巧来实现等效方法。
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 微秒,普通 python 需要 29.5 微秒。
我想用公式中的数据填充一个 24 x 4 数组。
基于 4 个初始值,如 [0, 0, 2137, 1419]
,数组应根据下面的输出 table 填充数字。
在 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])
你还没有回复我的评论。但是查看所需的输出和 Formulas: 之后的文本我想,你真的是想 add/subtract 1 而不是变量他们自己。
所以,您基本上是在前 7 行中重复添加向量 [1,0,1,0]
,然后在接下来的五行中添加 [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 或不想安装它,则可以使用一些 zip
和 map
技巧来实现等效方法。
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 微秒,普通 python 需要 29.5 微秒。