Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python数组:用数据填充数组的公式_Python_Arrays_Array Formulas - Fatal编程技术网

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

Python数组:用数据填充数组的公式,python,arrays,array-formulas,Python,Arrays,Array Formulas,我想用公式中的数据填充一个24 x 4的数组。 根据4个初始值,如[0,0,2137,1419],应根据下表的输出用数字填充数组 在Excel中,经常使用时,这很容易。但是当经常使用并且在a、b、c或d中的值发生变化时,让Python创建各种数组将非常有用 问题:如何在Python中实现这一点 我假设嵌套的I-in-j循环可以完成这项工作,但老实说,我在这里迷失了方向。非常感谢您的帮助 初始数据: a+和a-使用7行 b+和b-使用5行 a = 0 b = 0 c = 2137 d =

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

在Excel中,经常使用时,这很容易。但是当经常使用并且在a、b、c或d中的值发生变化时,让Python创建各种数组将非常有用

问题:如何在Python中实现这一点

我假设嵌套的I-in-j循环可以完成这项工作,但老实说,我在这里迷失了方向。非常感谢您的帮助

初始数据:

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
输出:


你还没有回复我的评论。但是看看期望的输出和公式后的文本:我想,你真正想要的是加/减1,而不是变量本身

所以,你基本上是在前7行中反复加上向量[1,0,1,0],然后在接下来的5行中再加上[0,1,0,1],然后再减去同样的东西。 这是很好的线性关系,因此您可以将它们累加起来,并将结果始终应用于第一行。这对numpy来说太棒了

这将非常快,对于大型阵列也是如此。但是,如果您没有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]]

我在笔记本电脑上测试了这两种方法的性能。求和已经建立,numpy需要6.29µs,而纯python需要29.5µs。

为什么a-=a,b-=b,c-=c和d-=d不变成0?我想你的意思是-=1。还有,你有numpy吗?太好了,谢谢!你的假设是正确的。在您的numPy解决方案中,我必须删除“列表”,代码工作得非常好。然后,当堆叠“itertools.chain对象”和“itertools.starmap对象”时,普通Python解决方案停止在“两者”处。。。我不知道vstack接受发电机。不过,不必从中删除列表。你犯了什么错误?当然,您应该将列表保留在纯python版本中。正如您所注意到的,如果没有它,您将得到一个链对象和一个星图对象,这两个对象都是生成器。这就是为什么我把它们变成了一个列表,你不能把生成器累加起来,一旦它们被消耗掉,你就不能再重复它们了。在查阅了np.vstack的手册后,我想按如下方式更改该行,结果成功:upper=np.vstackit.chainit.repeatadd1,7,it.repeatadd2,5。然而,这似乎不适用于纯Python解决方案。链对象和星图对象堆叠时都会卡住=上+下。msg is:TypeError:不支持+:'itertools.chain'和'itertools.starmap'的操作数类型。我在windows7-64计算机上的jupyter笔记本中工作。忘记在jupyter笔记本中提供错误消息:TypeError:“list”对象不可调用。当运行发布的两个代码选项时,会显示此消息。完整错误消息如下:TypeError Traceback最近一次调用10次中的最后一次相应地堆栈它们11 upper=np.vstacklistit.chainit.repeatadd1,7,-->12 it.repeatadd2,5 13 14 lower是upper TypeError的否定版本:“list”对象不可调用,这可能是因为您将列表命名为“list”。您应该尽量避免为变量使用内置函数的名称。它们在jupyter中以绿色突出显示;
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]]