0

I have this piece of code:

    def substract_mean(self, group_point):
    for i, a in enumerate(group_point):
        group_point[i] = group_point[i] - self.mean_global[i]
    return group_point

And I received the following error message:

group_point[i] = group_point[i] - self.mean_global[i]

TypeError: 'map' object is not subscriptable

Community
  • 1
  • 1
Sue
  • 1
  • 1
  • 2
  • 1
    **1** The title of your question doesn't match the code you've shown. **2** I suspect that you are trying to run old Python 2 code on a Python 3 interpreter. – PM 2Ring Oct 11 '17 at 03:05
  • Hi Sue, welcome to SO! Please could you fix the formatting of your code so we can see how you indented your code. As you have probably found out, proper indentation is absolutely necessary for the interpreter and us to understand what you're trying to do. – Stuart Buckingham Oct 11 '17 at 03:07
  • If your group_point variable can be successfully enumerated, chances are the issue is self.mean_global. Please put `print(type(self.mean_global))` in your code instead of the for loop and post the result – Stuart Buckingham Oct 11 '17 at 03:10
  • Possible duplicate of [Error " 'dict' object has no attribute 'iteritems' " when trying to use NetworkX's write\_shp()](https://stackoverflow.com/questions/30418481/error-dict-object-has-no-attribute-iteritems-when-trying-to-use-networkx) – wp78de Oct 11 '17 at 03:14

2 Answers2

1

If you are using Python 3, the map() function changed from Python 2. map() returns an iterator in Python 3, so if you wanted to convert that into a subscriptable list, you should contain your map() variables inside a list() function.

e.g. list(map(...))

Source: https://docs.python.org/3.0/whatsnew/3.0.html#views-and-iterators-instead-of-lists

rmutalik
  • 1,925
  • 3
  • 16
  • 20
0
def square(n):
    return n*n


class MeanClass:
    mean_global = [10, 20, 30, 40]

    def substract_mean(self, group_point):
        for i, a in enumerate(group_point):
            group_point[i] = group_point[i] - self.mean_global[i]
        return group_point


c = MeanClass()
l = [1,2,3,4]
m = map(square,l)
print('works on a list object', c.substract_mean(l))
print('works on map object converted to a list',c.substract_mean(list(m)))
print('fails on a map object',c.substract_mean(m))