3
I needed to pass a string
of the kind:
0.4350 0.8798 0.0099 1
for a list
[0.4350, 0.8798, 0.0099, 1]
with a script
simple. How can I do it?
3
I needed to pass a string
of the kind:
0.4350 0.8798 0.0099 1
for a list
[0.4350, 0.8798, 0.0099, 1]
with a script
simple. How can I do it?
7
If you have a string with "0.4350 0.8798 0.0099 1", you need to do two operations: separate the elements where there is blank space, and for each separate element, which will be a string, transform it into a float number.
The first operation will always be done by the "split" method of strings in Python. By default this method still has the advantage of considering any number of whitespace (and other spacing characters like newline, tabs, etc...) as a single space:
>>> a = "0.4350 0.8798 0.0099 1"
>>> b = a.split()
>>> b
['0.4350', '0.8798', '0.0099', '1']
For those who are in the first steps in Python, the easy way to understand to do the second operation is to create a blank list, and a for
that for each element in b
add your converted value to float in the new list:
>>> c = []
>>> for elemento in b:
... c.append(float(elemento))
...
>>> c
[0.435, 0.8798, 0.0099, 1.0]
But once people become more comfortable with Python, the ideal for this is to use a "list comprehension" - it’s an expression of the Pythonq sitnaxe that lets you create a list from an arbitrary sequence using a for
on the same line - as an expression. Your whole problem would be solved like this:
c = [float(elemento) for elemento in "0.4350 0.8798 0.0099 1".split()]
(The contents of c will be the same as in the previous example).
What this form does: first runs the expression "for" will use, after "in" - this is its initial string (which can be in a variable, of course), with the application of the method split
. There the for
is executed, and for each part of the sequence returned by the split, the expression before the for
, the float(elemento)
is executed, and its output becomes part of the final list assigned to the variable c
.
Finally, people who do not know Python well, but accustomed to languages that allow a "functional" approach, can present solutions using Python’s "map", which is a built-in function that takes as parameters a function and a sequence, it automatically processes all elements of the sequence passed in the function - and creates a new output sequence. In Python version 2, map output was a direct list - in Python 3, map
creates a generator, which must be transformed into a list:
c = list(map(float, "0.4350 0.8798 0.0099 1".split()))
The list-comprehension version in general is more expressive and easier to "think" than the functional syntax approach - but it does not prevent that because of personal taste to use the map
.
2
I’m not a developer python, but I managed to solve it that way:
Code:
str = '0.4350 0.8798 0.0099 1';
val = filter(None, str.split(' '));
print val
Exit:
['0.4350', '0.8798', '0.0099', '1']
def noEmpty(x):
return x != ""
str = '0.4350 0.8798 0.0099 1';
print (list(filter(noEmpty, str.split(' '))));
References:
This only works in Python 2.x - for questions where the answer does not mark the version explicitly, I would recommend using Python 3 - and in any case tell which version of Python was used.
@jsbueno thanks for the tip, I’m not phython developer but, what I did put in both versions, I hope it’s useful ... !!!
Cool - was worth the effort there - I hope the contact with Python to create these examples was cool. I put a very didactic response for beginners and complete.
0
To transform a string into a list, you just need to separate this string by a more timely character. In your case, the most opportune character is the space character. For this, just insert the value of the string, leaving it in the form...
a = '0.4350 0.8798 0.0099 1'
...and then display the result obtained by applying the method split() to the variable "to", that is to say...
print(a.split())
This way the complete code would look like this:
a = '0.4350 0.8798 0.0099 1'
print(a.split())
Now, if you also want to turn each element of the list into real numbers - float() - you can, with the help of List Comprehensions, transform the list - created before - into a list, in which each element of the list is a value float
.
This way the code would be:
a = '0.4350 0.8798 0.0099 1'
print([float(i) for i in a.split()])
The jsbueno response no longer presented this solution?
Browser other questions tagged python string date list
You are not signed in. Login or sign up in order to post.
Give the expected result Hugo?
– novic