Home > other >  'map' object is not subscriptable,How to extract a part of map
'map' object is not subscriptable,How to extract a part of map

Time:12-13

     66         test_indexes = set(random.sample(indexes, len(indexes)//2)) # removing 50% edges from test data
     67         train_indexes = set(indexes).difference(test_indexes)
---> 68         test_list = [edge_list[i] for i in test_indexes]
     69         train_list = [edge_list[i] for i in train_indexes]
     70         return train_list,test_list

TypeError: 'map' object is not subscriptable

I want to know how to take part of edgelist (which is a map) into test_list>Please help me with this

CodePudding user response:

So I believe that given the information, at some point you may have tried to create a list (edge_list) by mapping a function to another list using map. Please take the following as an example.

lst = [1,2,3]
new_lst = map(lambda x: x**2, lst)
type(new_lst)

This returns a 'map' object which is not iterable. Try casting the new_lst in this example, the edge_list in yours, to a list by:

lst = [1,2,3]
new_lst = list(map(lambda x: x**2, lst))
type(new_lst)
  • Related