Error in python 3.5: can't add `map` results together -


i have piece of code using python 2.7 , trying convert can use python 3.5 error on flowing code due map. best way resolve ?

file "/users/newbie/anaconda3/lib/python3.5/site-packages/keras/caffe/caffe_utils.py", line 74, in parse_network blobs = top_blobs + bottom_blobs

typeerror: unsupported operand type(s) +: 'map' , 'map'

 def parse_network(layers, phase):  '''     construct network layers making blobs ,  layers(operations) nodes. '''   nb_layers = len(layers)   network = {}     l in range(nb_layers):      included = false      try:         # try see if layer phase specific         if layers[l].include[0].phase == phase:             included = true      except indexerror:         included = true       if included:         layer_key = 'caffe_layer_' + str(l)  # actual layers, special annotation mark them         if layer_key not in network:             network[layer_key] = []         top_blobs = map(str, layers[l].top)         bottom_blobs = map(str, layers[l].bottom)         blobs = top_blobs + bottom_blobs          blob in blobs:             if blob not in network:                 network[blob] = []         blob in bottom_blobs:             network[blob].append(layer_key)         blob in top_blobs:                 network[layer_key].append(blob)  network = acyclic(network)  # convert acyclic  network = merge_layer_blob(network)  # eliminate 'blobs', have layers  return network 

map in python 3 return iterator, while map in python 2 returns list:

python 2:

>>> type(map(abs, [1, -2, 3, -4])) <type 'list'> 

python 3:

>>> type(map(abs, [1, -2, 3, -4])) <class 'map'> 

(note map bit more special, it's not list.)

you can't add iterators. can chain them, using itertools.chain:

>>> itertools import chain >>> chain(map(abs, [1, -2, 3, -4]), map(abs, [5, -6, 7, -8])) <itertools.chain object @ 0x7fe0dc0775f8> 

for final result, either have loop on chain, or evaluate it:

>>> value in chain(map(abs, [1, -2, 3, -4]), map(abs, [5, -6, 7, -8])): ...     print(value) ...  1 2 3 4 5 6 7 8 

or

>>> list(chain(map(abs, [1, -2, 3, -4]), map(abs, [5, -6, 7, -8]))) [1, 2, 3, 4, 5, 6, 7, 8] 

note former comes more naturally, , evaluating iterator last example should avoided unless absolutely necessary: can save memory , possibly computational power (for example, there break in loop, further values not have evaluated).


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -