![](/img/trans.png)
[英]python3: TypeError: 'generator' object is not subscriptable
[英]Python 'TypeError': 'Generator' object is not subscriptable
我正在嘗試在Python中執行一個簡單的Euclid示例,但收到標題中提到的錯誤。 代碼如下:
def gcd1(a,b): """ the euclidean algorithm """ while a: a, b = b%a, a return b
我正在按以下方式調用代碼(我認為這可能與它有關):
for x in set1: print(gcd1(x, set2[x]))
編輯:現狀(工作)
set1 = list(range(start, end)) """ otherrange() behaves just like range() however returns a fixed list""" set2 = list(otherrange(start, end)) for x in set1: print(gcd1(x, set2[x]))
這意味着set2
是一個生成器,要解決這個問題,只需將其變成一個列表即可。
set2_list = list(set2)
for x in set1:
print(gcd1(x, set2_list[x]))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.