phcaze
phcaze

Reputation: 1777

Python creating a lists of lists of floats from a single list of strings

I'm new to python and I need to create a list of lists (a matrix) of float values from a list of strings. So if my input is:

objectListData = ["1, 2, 3, 4", "5, 6, 7, 8", "9, 0, 0, 7", "5, 4, 3, 2", "2, 3, 3, 3", "2, 2, 3, 3"]

what I want to obtain is:

[[1, 2, 3, 4], [5, 6, 7, 8], [9, 0, 0, 7], [5, 4, 3, 2], [2, 3, 3, 3], [2, 2, 3, 3]]

Here's my code:

objectListData = ["1, 2, 3, 4", "5, 6, 7, 8", "9, 0, 0, 7", "5, 4, 3, 2", "2, 3, 3, 3", "2, 2, 3, 3"]


objectListDataFloats = [[0] * len(objectListData[0].split(', '))] * len(objectListData)
for count in range(1,len(objectListData)):
    for ii in range(1,len(objectListData[count].split(', '))):
        objectListDataFloats[count][ii] = float(objectListData[count].split(', ')[ii])

print objectListDataFloats

objectListDataFloats=[[0, 2.0, 3.0, 3.0], [0, 2.0, 3.0, 3.0], [0, 2.0, 3.0, 3.0], [0, 2.0, 3.0, 3.0], [0, 2.0, 3.0, 3.0], [0, 2.0, 3.0, 3.0]]

where is the error? I can't find it. Thanks

Upvotes: 1

Views: 1851

Answers (2)

Sukrit Kalra
Sukrit Kalra

Reputation: 34493

The problem is that your inner lists are references to one single list and not individual lists.

>>> objectListDataFloats = [[0] * len(objectListData[0].split(', '))] * len(objectListData)

>>> objectListDataFloats
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
>>> id(objectListDataFloats[0]) == id(objectListDataFloats[1])
True

After you fix that, you need to iterate from the starting index of 0, since the lists in Python start their index from 0.

for count in range(len(objectListData)):
    for ii in range(len(objectListData[count].split(', '))):
        objectListDataFloats[count][ii] = float(objectListData[count].split(', ')[ii])


>>> objectListDataFloats
[[1.0, 2.0, 3.0, 4.0], [5.0, 6.0, 7.0, 8.0], [9.0, 0.0, 0.0, 7.0], [5.0, 4.0, 3.0, 2.0], [2.0, 3.0, 3.0, 3.0], [2.0, 2.0, 3.0, 3.0]]

To completely do away with the initial initialization of the list with zeroes, you could also just build the list as you go along, something like

>>> objectListDataFloats = []
>>> for elem in objectListData:
        test_list = []
        for val in elem.split(','):
            test_list.append(float(val))
        objectListDataFloats.append(test_list)


>>> objectListDataFloats
[[1.0, 2.0, 3.0, 4.0], [5.0, 6.0, 7.0, 8.0], [9.0, 0.0, 0.0, 7.0], [5.0, 4.0, 3.0, 2.0], [2.0, 3.0, 3.0, 3.0], [2.0, 2.0, 3.0, 3.0]]

You don't need to iterate over the list or a string by using indices, you can just iterate over the list like in the above example.

Reduced Solution -

You could just reduce the whole solution to the following though (Change int to float if you require floating point numbers)

>>> objectListData = ["1, 2, 3, 4", "5, 6, 7, 8", "9, 0, 0, 7", "5, 4, 3, 2", "2, 3, 3, 3", "2, 2, 3, 3"]
>>> [map(int, elem.split(',')) for elem in objectListData]
[[1, 2, 3, 4], [5, 6, 7, 8], [9, 0, 0, 7], [5, 4, 3, 2], [2, 3, 3, 3], [2, 2, 3, 3]]

Upvotes: 2

user2555451
user2555451

Reputation:

Here ya go:

[[int(y) for y in x.split(",")] for x in objectListData]

output:

[[1, 2, 3, 4], [5, 6, 7, 8], [9, 0, 0, 7], [5, 4, 3, 2], [2, 3, 3, 3], [2, 2, 3, 3]]

or, if you want floats:

[[float(y) for y in x.split(",")] for x in objectListData]

output:

[[1.0, 2.0, 3.0, 4.0], [5.0, 6.0, 7.0, 8.0], [9.0, 0.0, 0.0, 7.0], [5.0, 4.0, 3.0, 2.0], [2.0, 3.0, 3.0, 3.0], [2.0, 2.0, 3.0, 3.0]]

Upvotes: 4

Related Questions