简体   繁体   中英

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:

[7][4.5][4]

However, I would like either an array or list that looks like this:

([7][4.5][4])

What functions in numpy or list will allow me to do this in python?

The logic are correct. The Chao list should to be initialized outside of the loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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