Python 3's map returns a map object, which you need to convert to list explicitly:
f1_b = list(map(lambda x: list(map(lambda t: t.strip(), x.split(',', 1))), lst))
Though in most cases you should prefer list comprehensions to map calls:
f1_a = [[t.strip() for t in x.split(',', 1)] for x in lst]
Answer from vaultah on Stack OverflowPython 3's map returns a map object, which you need to convert to list explicitly:
f1_b = list(map(lambda x: list(map(lambda t: t.strip(), x.split(',', 1))), lst))
Though in most cases you should prefer list comprehensions to map calls:
f1_a = [[t.strip() for t in x.split(',', 1)] for x in lst]
Python3 map function returns map object and that object need to typecast in list.
num = [" a , 1 ", " b , 2 "]
list1 = list(map(lambda x:x.split(','), num))
print(list1)
Output:
[[' a ', ' 1 '], [' b ', ' 2 ']]
python 3.x - Operating on Nested Tuples using Map function - Stack Overflow
"Map" a nested list in Python - Stack Overflow
The map() function or the list comprehension?
python - How to make map for a nested function? - Stack Overflow
So I have a list of characters. And let's say I need to get at each of the characters class types. Using map() in python 3, how exactly would one go about this? Here's the list of characters. I've tried going at this a few ways, but they have all failed. The real issue is that I'm not sure how to get at the nested dictionary.
chars = [{'Character1': {'age': 19, 'name': 'Balthazar',
'class': 'Wizard'}}, {'Character2': {'age': 24,
'name': 'Thadeus', 'class': 'Warrior'}}]
classes = list(map(lambda x: x.update({"classes":
list(map(lambda y: # need to get at each 'class' key
here to output to the list of classes), collection)}),
chars))EDIT: This is something I'm working on at work. I'm a Jr. Dev, and these made up dictionaries hold no value to what I'm actually working on whatsoever. I just need to understand how this works and am not coming up with much in my searching.
One-liner with mix of map and listcomp:
a = [map(float, suba) for suba in a] # Only works on Py2
Or variants:
# Both of the below work on Py2 and Py3
a = [list(map(float, suba)) for suba in a]
a = [[float(x) for x in suba] for suba in a]
Choose based on your personal preference and target Python version. For large nested lists on CPython 2, the first variant is probably the fastest (if the inner lists are huge, it avoids lookup overhead to get the float constructor and byte code execution for the inner lists), and the list wrapped equivalent might eventually win on CPython 3; for small nested lists on all versions, the nested list comprehensions is usually going to be the fastest.
With list comprehension:
a = [[float(j) for j in i] for i in a]
I am trying to use multiprocessing to speed up some processes in my code but I want it to be optional and I was as few changes as possible for the parallel version.
As such, I am trying to write everything as functions with the only difference being whether you use Python's map or multiprocessing.Pool.map()
This requires some use of nested functions but I keep getting the following error:
PicklingError: Can't pickle <type 'function'>:attribute lookup __builtin__.function failed
I think it has to do with nested functions. To try to demonstrate, I made the following working example:
import multiprocessing as mp
## Works
def fooA(q):
return q**2
pool = mp.Pool()
ResA_S = map(fooA,range(10))
ResA_P = pool.map(fooA,range(10))
pool.close()
assert ResA_S==ResA_P
## Fails
def bar():
def fooB(q):
return q**2
pool = mp.Pool()
ResB_S = map(fooB,range(10))
ResB_P = pool.map(fooB,range(10))
pool.close()
return ResB_S,ResB_P
ResB_S,ResB_P = bar()
assert ResB_S==ResB_PAny ideas on how I can fix this? Or work around it? I really want to have something like the following:
if doPar:
pool = mp.Pool()
res = pool.map(func,inputs)
pool.close()
else:
res = map(func,inputs)so that everything else remains the same.
Thoughts? Ideas?
Thanks!