简体   繁体   English

Python'TypeError':'Generator'对象不可下标

[英]Python 'TypeError': 'Generator' object is not subscriptable

I'm trying to perform a simple Euclid example in Python but receive the error mentioned in the title. 我正在尝试在Python中执行一个简单的Euclid示例,但收到标题中提到的错误。 The code is as follows: 代码如下:

def gcd1(a,b):
        """ the euclidean algorithm """
        while a:
                a, b = b%a, a
        return b

I'm calling the code as follows (i think this might have something to do with it): 我正在按以下方式调用代码(我认为这可能与它有关):

for x in set1:
    print(gcd1(x, set2[x]))

Edit: current situation (works) 编辑:现状(工作)

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]))

This means that set2 is a generator, to get around this just turn it into a list. 这意味着set2是一个生成器,要解决这个问题,只需将其变成一个列表即可。

set2_list = list(set2)
for x in set1:
    print(gcd1(x, set2_list[x]))

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

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