tags:

views:

328

answers:

7

What is the best way to convert a string on the format "1-2-3-4" to a list [1, 2, 3, 4]? The string may also be empty, in which case the conversion should return an empty list [].

This is what I have:

map(lambda x: int(x),
    filter(lambda x: x != '',
           "1-2-3-4".split('-')))

EDIT: Sorry all of those who answered before I corrected my question, it was unclear for the first minute or so.

+8  A: 
>>> for s in ["", "0", "-0-0", "1-2-3-4"]:
...     print(map(int, filter(None, s.split('-'))))
... 
[]
[0]
[0, 0]
[1, 2, 3, 4]
Nadia Alramli
I'm sorry, I corrected my question now.
Deniz Dogan
@blahblah, I updated my answer too
Nadia Alramli
This won't adress the empty sting case.
voyager
@voyager: I just updated the answer to fix that. (Hope you're OK with that, Nadia.)
Stephan202
@voyager: thanks!
Nadia Alramli
`filter(bool, ..)` or `filter(len, ..)` might be easier to read than `filter(None, ..)`.
J.F. Sebastian
+12  A: 

You can use a list comprehension to make it shorter. Use the if to account for the empty string.

the_string = '1-2-3-4'

[int(x) for x in the_string.split('-') if x != '']
Rudd Zwolinski
why are you checking for empty string like this?
SilentGhost
SilentGhost, the question says that in the case of an empty string it should return an empty list: "The string may also be empty, in which case the conversion should return an empty list []."
Rudd Zwolinski
so why do you do it the way you do it? you're not aware of what object evaluate to False?
SilentGhost
`>>> [x for x in ''.split('-')]`\n `['']`\n `>>> [x for x in '-'.split('-')]`\n `['', '']`
voyager
`>>> [int(x) for x in ''.split('-') if x != '']` \n `[]`
voyager
`>>> [int(i) for i in ''.split('-') if i]`
SilentGhost
Good point. The != '' part is pointless. +1 to your comment. But still, it might be good for clarity.
Rudd Zwolinski
+4  A: 

Convert the higher-order functions to a more readable list-comprehension

[ int(n) for n in "1-2-3-4".split('-') if n != '' ]

The rest is fine.

Dario
+2  A: 

From the format of your example, you want int's in the list. If so, then you will need to convert the string numbers to int's. If not, then you are done after the string split.

text="1-2-3-4"

numlist=[int(ith) for ith in text.split('-')]
print numlist
[1, 2, 3, 4]

textlist=text.split('-')
print textlist
['1', '2', '3', '4']

EDIT: Revising my answer to reflect the update in the question.

If the list can be malformed then "try...catch" if your friend. This will enforce that the list is either well formed, or you get an empty list.

>>> def convert(input):
...     try:
...         templist=[int(ith) for ith in input.split('-')]
...     except:
...         templist=[]
...     return templist
...     
>>> convert('1-2-3-4')
[1, 2, 3, 4]
>>> convert('')
[]
>>> convert('----1-2--3--4---')
[]
>>> convert('Explicit is better than implicit.')
[]
>>> convert('1-1 = 0')
[]
semiuseless
A: 
def convert(s):
    if s:
        return map(int, s.split("-"))
    else:
        return []
mipadi
A: 

you don't need the lambda, and split won't give you empty elements:

map(int, filter(None,x.split("-")))
fortran
Unfortunately, that breaks on empty strings.
mipadi
hmmm... I didn't noticed that split would return an empty string if the input was an empty string (I thought it returned an empty list). Fixed with a filter
fortran
+1  A: 

I'd go with this:

>>> the_string = '1-2-3-4- -5-   6-'
>>>
>>> [int(x.strip()) for x in the_string.split('-') if len(x)]
[1, 2, 3, 4, 5, 6]
hughdbrown