Question

Out of a list in the file, i am trying to get 2 divided new lists: Subset A, and Subset B. By that means the elements (integers) in Subset A should be equal to Subset B. (This program uses backtracking to solve the problem by the way.) However i am getting:

Subset A: <map object at 0x311b110> 
Subset B: <map object at 0x311b190>

and an error:

     line 93, in getSuccessors
   cfgA.subA.append(config.input[config.index])
TypeError: 'map' object is not subscriptable

The constructor function that i indicated the map in is:

def mkPartitionConfig(filename):
  """
  Create a PartitionConfig object.  Input is:
      filename   # name of the file containing the input data

  Returns: A config (PartitionConfig)
  """


  pLst = open(filename).readline().split()
  print(pLst)
  config = PartitionConfig
  config.input = map(int, pLst)
  config.index = 0
  config.subA = []
  config.subB = []
  return config

and the function where i am getting an error:

def getSuccessors(config):
  """
  Get the successors of config.  Input is:
      config: The config (PartitionConfig)
  Returns: A list of successor configs (list of PartitionConfig)
  """

  cfgA = deepcopy(config)
  cfgB = deepcopy(config)
  cfgA.subA.append(config.input[config.index])
  cfgB.subB.append(config.input[config.index])
  cfgA += 1
  cfgB += 1

  return [configA, configB]

What am i doing wrong in here?

Was it helpful?

Solution

As the error message suggests, you cannot subscript (square bracket) a map object. Maps are a type of iterable in python, meaning the only way to get data out of them is to iterate through them one element at a time. If you want to subscript it, you need to store it as a list.

config.input = list(map(int, pLst))

As this simple example shows, you cannot subscript a map object.

>>> x = [0, 1, 23, 4, 5, 6, 6]
>>> y = map(str, x)
>>> y
<map object at 0x02A69DB0>
>>> y[1]
Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    y[1]
TypeError: 'map' object is not subscriptable

And to get data out of the map object:

>>> for i in y:
    print(i)


0
1
23
4
5
6
6

OTHER TIPS

You need to convert the map object to a list.

list(yourmapobject)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top