简体   繁体   English

python3:TypeError:'generator' object 不可下标

[英]python3: TypeError: 'generator' object is not subscriptable

I am very new to python.我对 python 很陌生。 I am trying to run a simple code with a 2d list.我正在尝试使用 2d 列表运行一个简单的代码。 But I'm getting an error: "TypeError: 'generator' object is not subscriptable".但我收到一个错误:“TypeError: 'generator' object is not subscriptable”。 Can anyone please help me with how to solve this?谁能帮我解决这个问题? Or what is wrong with the code.或者代码有什么问题。

import sys
import numpy as np
from scipy import stats

max_event = 1000000
a_bin = 10  # number each bin from 0-->10 where cumulant calculation will be done

# Define 2D array for [ bin, here 0->10][proton in each bin]
pArray = (() for nn in range(a_bin))
neve = (0 for mm in range(a_bin))

for ii in range(0, max_event):

    _a = np.random.randint(10)
    _b = np.random.randint(120)

    if ii % 1000 == 0:
        print(ii, _a, _b)

    for j in range(0, 10):
        if _a == j:
            pArray[j].append(_b)
            neve[j] += 1


print("filling done!")

for k in range(0, a_bin):

    mu2 = stats.mstats.moment(pArray[k], moment=2)
    mu4 = stats.mstats.moment(pArray[k], moment=4)

    print('serial = %d, mu_2 = %f , mu_4 = %f, event = %d' %
          (k, mu2, mu4, neve[k]))
    # print k, neve[k], c1[k], c2[k], c3[k], c4[k], c5[k], c6[k]

print("calculation done!")

Here is the output I am getting:这是我得到的 output:

0 9 18 Traceback (most recent call last): File "calcumuBin.py", line 23, in pArray[j].append(_b) TypeError: 'generator' object is not subscriptable 0 9 18 Traceback(最近一次调用最后):文件“calcumuBin.py”,第 23 行,在 pArray[j].append(_b) 类型错误:'generator' object 不可下标

You can change the pArray to be a list of lists that will prevent you from getting the TypeError: 'generator' object is not subscriptable您可以将 pArray 更改为列表列表,以防止您收到TypeError: 'generator' object is not subscriptable

pArray = [[] for nn in range(a_bin)]
neve = [0 for mm in range(a_bin)]

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

相关问题 Python'TypeError':'Generator'对象不可下标 - Python 'TypeError': 'Generator' object is not subscriptable TypeError: 'coroutine' object 不可订阅 | Python3 - TypeError: 'coroutine' object is not subscriptable | Python3 TypeError:“函数”对象不可下标[Python3] - TypeError: 'function' object is not subscriptable[Python3] 类型错误:“int”对象不可下标 - Python3 - TypeError: 'int' object is not subscriptable - Python3 TypeError: 'int' object 在 Python3 中不可下标 - TypeError: 'int' object is not subscriptable in Python3 类型错误:“生成器”对象不可下标错误 - TypeError: 'generator' object is not subscriptable error python3 / pymongo类中的“ TypeError:'NoneType'对象不可下标” - python3 / pymongo “TypeError: 'NoneType' object is not subscriptable” within class python3字典get.key-TypeError:“ int”对象不可下标 - python3 dictionary get.key - TypeError: 'int' object is not subscriptable 从Python 2.7切换到3.7错误:“ TypeError:'generator'对象不可下标” - Switching from Python 2.7 to 3.7 error: “TypeError: 'generator' object is not subscriptable” TypeError:“发电机”对象不可下标,CSV文件 - TypeError: 'generator' object is not subscriptable, csv file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM