tags:

views:

115

answers:

6

Hi, I have a string like this:

'|Action and Adventure|Drama|Science-Fiction|Fantasy|'

How can I convert it to a tuple or a list?

Thanks.

+1  A: 

You want str.split():

>>> s = '|Action and Adventure|Drama|Science-Fiction|Fantasy|'
>>> s.split('|')
['', 'Action and Adventure', 'Drama', 'Science-Fiction', 'Fantasy', '']
jathanism
+1  A: 

Hi,

If you want to just split your string at the | character you use:

myStr.split('|')

If you also want all zero-length element removed (like the ones from the ends) you:

def myFilter(el): return len(el) > 0
filter(myFilter, myStr.split('|'))
Alin Purcaru
Thanks jathanism and Alin.
Antonio Pardo
Thanks won't work here ;-), Set this answer as your accepted answer" If so :)
Tumbleweed
`[ x for x in myStr.split('|') if len(x) > 0 ]` is much cleaner.
Jack Kelly
+5  A: 
>>> s = '|Action and Adventure|Drama|Science-Fiction|Fantasy|'
>>> 
>>> [item for item in s.split('|') if item.strip()]
['Action and Adventure', 'Drama', 'Science-Fiction', 'Fantasy']
>>> 

If you'd rather have a tuple then:

>>> tuple(item for item in s.split('|') if item.strip())
('Action and Adventure', 'Drama', 'Science-Fiction', 'Fantasy')
>>> 
Manoj Govindan
Also note that you should (probably) not use tuples in your case because you want multiple single component items, not one item with variable number of components.
Alin Purcaru
@Alin: Didn't understand that. Care to explain?
Manoj Govindan
I wanted to point out that it is not semantically correct to use tuples when you actually need lists.
Alin Purcaru
Not semantically correct? Not sure why you say that, for two reasons (1) The OP specifically asked for a tuple _or_ a list. (2) unless you are planning to modify the sequence later, a tuple would do just as fine as a list.
Manoj Govindan
@Alin Purcaru Tuples are just ordered lists of elements - they _are_ lists. If there's no need to use any of `list`'s methods, then `tuple` is a fine, concise choice of data storage.
Tim McNamara
A tuple(it doesn't matter how a programming language implements it) is an n-pair. When you say pair you know that the elements are connected and not independent. By analogy the components of a tuple (I don't name them items because they're not items) need to have a logical connection. On the other hand a list is just some items, you can add or remove and you will have the same object, but with more or less items. Example: (x1,x2) is a point in 2D, but (x1,x2,x3) - the same tuple with one element added - represents a point in 3D; [2,3,5] is a list of prime numbers, just like [2,3,5,7] and so on
Alin Purcaru
`By analogy the components of a tuple (I don't name them items because they're not items) need to have a logical connection`: I beg to differ. To quote wikipedia: `In mathematics and _computer science_, a tuple is an ordered list of elements.`. That definition doesn't require the components to have a logical connection.
Manoj Govindan
By being ordered they have a connection.
Alin Purcaru
Following that logic lists should be fine since lists are also defined to be ordered: `In computer science, a list or sequence is an abstract data structure that implements an ordered collection of values`. http://en.wikipedia.org/wiki/List_(computing)
Manoj Govindan
Alin Purcaru's argument is the one supported by Guido. Tuples are like records. Lists are sequences of objects of the same type.
FogleBird
I thought that seeing the difference between tuples and lists was common sense and I did not expect to generate such a comment **list** :).
Alin Purcaru
What, you don't mean a tuple of comments? ;) Seriously, yes, I see your point. Thanks for commenting.
Manoj Govindan
+1  A: 

Strip 'string'.strip('|')

   >>> heading = '|Action and Adventure|Drama|Science-Fiction|Fantasy|'
   >>> tuple(heading.strip('|').split('|'))
   ('Action and Adventure', 'Drama', 'Science-Fiction', 'Fantasy')

Slice 'string'[1:-1]

   >>> heading = '|Action and Adventure|Drama|Science-Fiction|Fantasy|'
   >>> tuple(heading[1:-1].split('|'))
   ('Action and Adventure', 'Drama', 'Science-Fiction', 'Fantasy')

For List remove the tuple() call.

kevpie
+1  A: 

strip() gets rid of the leading and trailing chars, split() divvies up the remainder:

>>> s.strip('|').split('|')
['Action and Adventure', 'Drama', 'Science-Fiction', 'Fantasy']
Mark Tolonen
A: 

List

seq = '|Action and Adventure|Drama|Science-Fiction|Fantasy|'.split('|')

Tuple

seq = tuple(seq)

If you want to strip empty items, pass the output through filter(None, seq). If you assume outer | always, just slice with seq[1:-1].

Matt Joiner