[英]Type Error: 'generator' object is not subscriptable in python
def activity_select(i):
list(i)
i.sort(key=lambda x: x[2])
n = len(i) # set n to length of i
solution = []
print("Following activities are selected ")
j = 0 # first activity always selected
solution.append(i[j][0])
for k in range(n): # loop through remainder of activities
if i[k][1] >= i[k][2]: # if activity has greater start time
solution.append(i[k][0]) #
j = k
return solution
file = open('act.txt')
st = []
sn = 1
for line in file:
l = line.rstrip()
if '' not in l and len(st) > 0:
print("Set", sn)
solution = activity_select(st)
print("Number of activities selected = ", len(solution))
print("Activities: ", *solution)
sn += 1
st = []
else:
st.append((int(i) for i in l.split()))
if len(st) > 0:
print("Set ", sn)
sol = activity_select(st)
print("Number of activities selected = ", len(sol))
print("Activities: ", *solution)
However I am getting the following errors 但是我收到以下错误
line 27, in <module>
sol = activity_select(st)
line 9, in activity_select
sol.append(v[i][0])
TypeError: 'generator' object is not subscriptable
Is this related to the st
variable? 这和
st
变量有关吗? Or is it something else? 或者是别的什么?
I see a couple obvious errors here: 我在这里看到几个明显的错误:
i
argument to a list
and didn't assign it back to i
, so if it's not a list
, the function is broken from the start. i
参数转换为list
,但没有将其分配回i
,因此,如果它不是 list
,则函数从一开始就被破坏。 Use i = list(i)
to replace i
with the result of listifying i = list(i)
用i = list(i)
结果替换i
st.append((int(i) for i in l.split()))
which is calling append
with a generator (created via a generator expression). st.append((int(i) for i in l.split()))
,后者使用生成器(通过生成器表达式创建st.append((int(i) for i in l.split()))
调用append
。 If it's supposed to be a list
, use brackets, not parens: st.append([int(i) for i in l.split()])
. list
,请使用方括号,而不要使用括号: st.append([int(i) for i in l.split()])
。 If each result should be added separately, use extend
instead: st.extend(int(i) for i in l.split())
extend
: st.extend(int(i) for i in l.split())
Problem #2 is almost certainly the source of your current error, but fixing #1 is important for making your code usable with non-lists (and to avoid having it mutate the caller's copy of the list
even when it works). 问题#2几乎可以肯定是当前错误的根源,但是修复问题#1对于使您的代码可用于非列表很重要(并且即使它起作用,也要避免使它改变
list
的调用者的副本),这一点很重要。
It is hard to debug your code seeing that you are reading of a file we do not have. 看到您正在读取我们没有的文件,很难调试您的代码。 My guess would be you are trying slice a generator, a quick glance shows that you have a generator expression here:
我的猜测是您正在尝试对生成器进行切片,快速浏览一下即可发现您在此处具有生成器表达式:
st.append((int(i) for i in l.split()))
Try changing it to st.append([int(i) for i in l.split()])
to get a nested list. 尝试将其更改为
st.append([int(i) for i in l.split()])
以获取嵌套列表。
Basically the problem with your code is: 基本上,您的代码存在以下问题:
st.append((int(i) for i in l.split()))
because you're appending a generator with is un-indexable, un-sliceable , un-subscriptable , so use a list comprehension which is indexable, sliceable, subscriptable. 因为要附加的生成器具有不可索引的,不可切片的,不可下标的,所以请使用可索引的,可切片的,可下标的列表理解。
So instead of: 所以代替:
st.append((int(i) for i in l.split()))
Do: 做:
st.append([int(i) for i in l.split()])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.