简体   繁体   中英

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. 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_list = list(set2)
for x in set1:
    print(gcd1(x, set2_list[x]))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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