“Map” a nested list in Python

后端 未结 4 889
自闭症患者
自闭症患者 2021-02-14 05:07

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         


        
4条回答
  •  梦如初夏
    2021-02-14 05:33

    Under the hood, map is a loop too. You should just use a list comprehension, however there is a nice way to do it with Python2 - you just need a partial function

    >>> a = [['2.3','.2'],['-6.3','0.9']]
    >>> from functools import partial
    >>> map(partial(map, float), a)
    [[2.3, 0.2], [-6.3, 0.9]]
    

    I don't think there is a nice way to convert this to Python3 though

    >>> list(map(list, map(partial(map, float), a)))  #YUKKK!
    [[2.3, 0.2], [-6.3, 0.9]]
    

提交回复
热议问题