0

I have an Arduino, that give me the below values.
I can read this strings matrix in Python, but I need to convert this values to floats matrix.

['-0.4152832031\t0.1403808593\t-0.1831054687\t-1.0670732259\t-0.0609756088\t0.0000000000\t185\t-1\t-683\r\n']
['-0.4165039062\t0.1406250000\t-0.1867675781\t-0.8841463088\t0.1524390220\t-0.0304878044\t186\t-2\t-685\r\n']
['-0.4140625000\t0.1411132812\t-0.1843261718\t-0.8536585807\t0.4573170661\t-0.2743902444\t186\t-1\t-686\r\n']
['-0.4165039062\t0.1408691406\t-0.1870117187\t-1.0365853309\t0.0304878044\t-0.1219512224\t185\t-2\t-685\r\n']
['-0.4165039062\t0.1389160156\t-0.1857910156\t-1.0060975551\t0.0304878044\t0.0000000000\t183\t0\t-683\r\n']
['-0.4147949218\t0.1396484375\t-0.1862792968\t-0.9756097793\t0.0609756088\t0.0304878044\t184\t0\t-684\r\n']
['-0.4147949218\t0.1401367187\t-0.1872558593\t-1.0670732259\t0.1524390220\t-0.1219512224\t185\t-2\t-685\r\n']
['-0.4147949218\t0.1381835937\t-0.1862792968\t-0.9756097793\t0.0304878044\t-0.1829268360\t185\t0\t-683\r\n']
['-0.4135742187\t0.1391601562\t-0.1887207031\t-0.9756097793\t0.1219512224\t-0.1219512224\t184\t-1\t-684\r\n']
['-0.4160156250\t0.1394042968\t-0.1848144531\t-0.9146341323\t-0.0304878044\t0.0000000000\t184\t-2\t-685\r\n']
['-0.4167480468\t0.1384277343\t-0.1855468750\t-1.0975609970\t0.0304878044\t0.0914634132\t183\t-2\t-686\r\n']
['-0.4155273437\t0.1381835937\t-0.1860351562\t-0.8841463088\t0.0000000000\t-0.1524390220\t184\t-4\t-685\r\n']
['-0.4162597656\t0.1396484375\t-0.1845703125\t-0.9451219558\t0.1524390220\t-0.1524390220\t186\t-1\t-686\r\n']
['-0.4167480468\t0.1406250000\t-0.1870117187\t-1.0975609970\t0.0000000000\t0.0304878044\t184\t-2\t-684\r\n']
['-0.4162597656\t0.1369628906\t-0.1838378906\t-1.0060975551\t-0.0914634132\t-0.0304878044\t185\t-2\t-684\r\n']
['-0.4155273437\t0.1384277343\t-0.1865234375\t-1.4634146690\t0.0000000000\t0.1219512224\t185\t-1\t-683\r\n']

1 Answer 1

1

In order to convert your string to the list of float numbers, you may use a list comprehension expression with str.split() and type-cast each value to float as:

>>> num_str = '-0.4152832031\t0.1403808593\t-0.1831054687\t-1.0670732259\t-0.0609756088\t0.0000000000\t185\t-1\t-683\r\n'

>>> [float(n) for n in num_str.split()]
[-0.4152832031, 0.1403808593, -0.1831054687, -1.0670732259, -0.0609756088, 0.0, 185.0, -1.0, -683.0]

OR, you may use map() to get the same result as:

>>> map(float, num_str.split())
[-0.4152832031, 0.1403808593, -0.1831054687, -1.0670732259, -0.0609756088, 0.0, 185.0, -1.0, -683.0]

Note: map() returns generator object in Python 3.x. In order to convert it to list, you have to explicitly type-cast the value to list using list(map(..., ...))

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you a lot for your help. I'm new in Python and some codes are new for me.

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.