[英]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.