1

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

1
  • 3
    In addition to the problems Sukrit Kalra's answers points out: Whenever you find yourself doing for i in range(len(foo)), you're probably doing it wrong. If you just want each value in foo, just do for value in foo. If you need the index as well as the value, do for index, value in enumerate(foo). Either way, you completely avoid the potential for off-by-one errors (like the one you actually have in your code), and make everything simpler and more readable. Commented Aug 9, 2013 at 18:29

2 Answers 2

4

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]]
Sign up to request clarification or add additional context in comments.

1 Comment

He describes his input as a matrix of float values, so assuming that every digit is a separate number seems unwarranted. Especially since it's unnecessary, given that the numbers are obviously comma-separated.
2

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]]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.