In Python, I am trying to apply an operator to a two layer nested array. For example,
a = [[\'2.3\',\'.2\'],[\'-6.3\',\'0.9\']]
for j in range(2)
for i in r
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 list
s), 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.