简体   繁体   English

从python中的循环将值附加到单个列表或数组

[英]append values to a single list or array from a loop in python

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

when I print Chao, the product of this loop I currently have this: 当我打印Chao时,此循环的产物当前是这样的:

[7][4.5][4] [7] [4.5] [4]

However, I would like either an array or list that looks like this: 但是,我想要一个看起来像这样的数组或列表:

([7][4.5][4]) ([7] [4.5] [4])

What functions in numpy or list will allow me to do this in python? numpy或list中的哪些函数将允许我在python中执行此操作?

The logic are correct. 逻辑是正确的。 The Chao list should to be initialized outside of the loop. Chao列表应在循环外部初始化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM