使用 numpy 函数获取列表并插入循环
我需要一些帮助来迭代 numpy 函数的循环。首先,我开始计算我正在寻找的所有值并将它们存储在字典中。
calculations[keys[0]] = [np.mean(matrix, axis=0),np.mean(matrix, axis=1),np.mean(list)]
calculations[keys[1]] = [np.var(matrix, axis=0),np.var(matrix, axis=1),np.var(list)]
calculations[keys[2]] = [np.std(matrix, axis=0),np.std(matrix, axis=1),np.std(list)]
calculations[keys[3]] = [np.max(matrix, axis=0),np.max(matrix, axis=1),np.min(list)]
calculations[keys[4]] = [np.min(matrix, axis=0),np.min(matrix, axis=1),np.max(list)]
calculations[keys[5]] = [np.sum(matrix, axis=0),np.sum(matrix, axis=1),np.sum(list)]
然后我想是否有更好的战争可以通过循环来做到这一点。我尝试了一些 f 字符串的东西,但没有成功。
list = [i for i in range(9)]
matrix = np.reshape(list,(3,3))
keys = ['mean','variance','standard deviation', 'max','min','sum']
operator = ['np.mean','np.var','np.std','np.max','np.min','np.sum']
calculations = {}
for i in range[len(keys)]:
calculations[keys[i]] = [f'{operater[0]}'(matrix, axis=0),f'{operater[0]}'(matrix, axis=1),f'{operater[0]}'(list)]
还有路可走吗?
提前致谢
i need some help iterating through a loop of numpy functions. First I started to calculate all the values i was looking for and stored them in a dictionary.
calculations[keys[0]] = [np.mean(matrix, axis=0),np.mean(matrix, axis=1),np.mean(list)]
calculations[keys[1]] = [np.var(matrix, axis=0),np.var(matrix, axis=1),np.var(list)]
calculations[keys[2]] = [np.std(matrix, axis=0),np.std(matrix, axis=1),np.std(list)]
calculations[keys[3]] = [np.max(matrix, axis=0),np.max(matrix, axis=1),np.min(list)]
calculations[keys[4]] = [np.min(matrix, axis=0),np.min(matrix, axis=1),np.max(list)]
calculations[keys[5]] = [np.sum(matrix, axis=0),np.sum(matrix, axis=1),np.sum(list)]
Then I thought is there a better war to do this, via a loop. I tried some f string things, but it didn't work out.
list = [i for i in range(9)]
matrix = np.reshape(list,(3,3))
keys = ['mean','variance','standard deviation', 'max','min','sum']
operator = ['np.mean','np.var','np.std','np.max','np.min','np.sum']
calculations = {}
for i in range[len(keys)]:
calculations[keys[i]] = [f'{operater[0]}'(matrix, axis=0),f'{operater[0]}'(matrix, axis=1),f'{operater[0]}'(list)]
is there a way to go?
thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
list
是一个关键字,因此不应用作变量,此外,您需要使用圆括号()
来表示range
,而不是方()
括号。 code>[] 的。在 Python 中,函数是一等成员,因此您只需传递它们并将它们保存在列表中即可。只需这样做:
list
is a keyword and therefore should not be used as a variable, furthermore you need to use round()
brackets forrange
, not square[]
ones.In Python, functions are first class members, so you can just pass them around and save them in a list. Just do this: