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

标签 python arrays for-loop numpy append

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/

相关文章:

python - 删除 django admin 的foreignkey字段中的默认选择

python - 为什么内置的 python 帮助功能不显示所有可能的命令?

javascript - 如何在中间停止 For 循环并从那里继续返回 JavaScript

javascript - 为什么我只从 javascript for 循环中获取最后一项?

python - 通过替换对 python 数组进行混洗

regex - 在 for 循环中将 shell 变量传递给 awk

python - Zipline:泡菜回测以恢复模拟

python - 按值或按引用传递

python - Python中声明实例变量的两种方式?

c - 在C中高效地遍历排序数组