使用 numpy 函数获取列表并插入循环

发布于 2025-01-10 04:15:11 字数 1085 浏览 0 评论 0原文

我需要一些帮助来迭代 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

玩物 2025-01-17 04:15:12

list 是一个关键字,因此不应用作变量,此外,您需要使用圆括号 () 来表示 range,而不是方 () 括号。 code>[] 的。
在 Python 中,函数是一等成员,因此您只需传递它们并将它们保存在列表中即可。只需这样做:

import numpy as np

numbers = list(range(9))

matrix = np.reshape(numbers,(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]] = [operator[i](matrix, axis=0),operator[i](matrix, axis=1),operator[i](numbers)]

list is a keyword and therefore should not be used as a variable, furthermore you need to use round () brackets for range, 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:

import numpy as np

numbers = list(range(9))

matrix = np.reshape(numbers,(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]] = [operator[i](matrix, axis=0),operator[i](matrix, axis=1),operator[i](numbers)]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文