gpt4 book ai didi

python - 将值从 python 循环 append 到单个列表或数组

转载 作者:太空宇宙 更新时间:2023-11-03 19:19:15 27 4
gpt4 key购买 nike

import numpy as np
import itertools
a = np.array([[1, 1, 3, 0, 0, 3, 2], [1, 3, 0, 0, 0, 3, 2], [1, 1, 10, 0, 0, 1, 0]])
for row in a:
sites = a.shape[0]
species = a.shape[1]
Chao = []
sing = np.where(row == 1, 1, 0)
doub = np.where(row == 2, 1, 0)
spec = np.where(row > 0, 1, 0)
F1 = (sum(sing))**2
F2 = float((sum(doub)))*2
Sobs = sum(spec)
if F2 == 0:
Ch = Sobs
else:
Ch = Sobs + (F1/F2)
Chao.append(Ch)
print Chao

当我打印 Chao 时,这个循环的乘积我当前有这个:

[7][4.5][4]

但是,我想要一个如下所示的数组或列表:

([7][4.5][4])

numpy 或 list 中的哪些函数允许我在 python 中执行此操作?

最佳答案

逻辑是正确的。 Chao 列表应该在循环外初始化。

关于python - 将值从 python 循环 append 到单个列表或数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10327235/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com