python: convert 2 dimension list of string to float

▼魔方 西西 提交于 2019-12-30 07:15:31

问题


I have a 2 dimension list of type string I'm trying to convert it to int. Things I've tried so far:

[[float(i) for i in lst[j] for j in lst]

with a for loop:

for i in range(len(lst)):
    for j in range(len(lst[i])):
         lst[i][j]=float(lst[i][j])

回答1:


>>> nums = [['4.58416458379', '3.40522046551', '1.68991195077'], ['3.61503670628', '5.64553650642', '1.39648965337'], ['8.02595866276', '8.42455003038', '7.93340754534']]
>>> [[float(y) for y in x] for x in nums]
[[4.58416458379, 3.40522046551, 1.68991195077], [3.61503670628, 5.64553650642, 1.39648965337], [8.02595866276, 8.42455003038, 7.93340754534]]

I'm not sure what your intention is but since you claim the for loop version didn't work, maybe you want a one dimensional result, in that case:

>>> [float(y) for x in nums for y in x]
[4.58416458379, 3.40522046551, 1.68991195077, 3.61503670628, 5.64553650642, 1.39648965337, 8.02595866276, 8.42455003038, 7.93340754534]



回答2:


Both of your solutions are close to working. Indeed, the second one should work already, though it can perhaps be improved a little bit.

Here's how I'd update the list comprehension version. The (perhaps dubious) advantage of this version is that it creates a new nested list containing floats, and leaves the original nested list of strings unchanged (so you can use it for something else, if you need to):

new_lst = [[float(string) for string in inner] for inner in lst]

Here's how I'd update your for loop code. Unlike the version using a list comprehension, this code modifies the contents of the original list of lists in place. While your current code should work just fine, the version below is somewhat more "pythonic" in my opinion, since it iterates over the contents of the list, rather than iterating over ranges and indexing the lists. Indexing is only needed for the assignment, and we get the index along with the string using the enumerate built-in function:

for inner in lst:
    for index, string in enumerate(inner):
        inner[index] = float(string)



回答3:


Using map(), here's a simplified example

nums = [['4.58416458379','1.68991195077'], ['8.02595866276'],
    ['3.61503670628', '5.64553650642', '1.39648965337']]

def myFloat(myList):
    return map(float, myList)

map(myFloat, nums)



回答4:


newList = []
listSize = len(oldList)
for i in range(0, listSize):
    newList.append([])
    for j in range(0, 2):
        newList[i].append(float(oldData[i][j]))

This helped me, this makes a new list called newList and type casts all the elements from oldList into newList. Although newList is initialised a 1-dimensional array, line 4 adds new dimensions according to whatever dimension your to be converted array is.




回答5:


correct your first solution to:

>>> sss = [['4.584','1.689'],['8.025'],['3.615','5.645','1.396']]
>>> fff = [float(s) for ss in sss for s in ss]      #notice the order of loops
>>> fff
[4.584, 1.689, 8.025, 3.615, 5.645, 1.396]

it works.



来源:https://stackoverflow.com/questions/15996596/python-convert-2-dimension-list-of-string-to-float

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!