Go Back   Velocity Reviews > Newsgroups > Python
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

Python - how best to split into singleton and sequence

 
Thread Tools Search this Thread
Old 10-18-2005, 11:36 PM   #1
Default how best to split into singleton and sequence


>>> l = []
>>> s = 'a|b'
>>> t, l = s.split('|')
>>> t

'a'
>>> l

'b'
>>> s = 'a|b|c|d'
>>> t, l = s.split('|')

Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: too many values to unpack
>>>


so, i imagine what is happening is the lhs, t,l, is really
(t, (l)), i.e. only two items.

so how should i have done this readably and simply?

randy



Randy Bush
  Reply With Quote
Old 10-19-2005, 12:23 AM   #2
George Sakkis
 
Posts: n/a
Default Re: how best to split into singleton and sequence
"Randy Bush" <> wrote:

> >>> l = []
> >>> s = 'a|b'
> >>> t, l = s.split('|')
> >>> t

> 'a'
> >>> l

> 'b'
> >>> s = 'a|b|c|d'
> >>> t, l = s.split('|')

> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> ValueError: too many values to unpack
> >>>

>
> so, i imagine what is happening is the lhs, t,l, is really
> (t, (l)), i.e. only two items.
>
> so how should i have done this readably and simply?


>>> s = 'a|b|c|d'
>>> l = s.split('|')
>>> t = l.pop(0)


By the way, don't use 'l' as an identifier; it is very close to '1' visually.

George




George Sakkis
  Reply With Quote
Old 10-19-2005, 02:40 AM   #3
Erik Max Francis
 
Posts: n/a
Default Re: how best to split into singleton and sequence
Randy Bush wrote:

> so, i imagine what is happening is the lhs, t,l, is really
> (t, (l)), i.e. only two items.
>
> so how should i have done this readably and simply?


Your question isn't at all clear. You're trying to assign a 4-element
tuple to two elements. That generates a ValueError.

Did you want to only split once at most? Then it's s.split('|', 1).
Did you want to assign the first element to the first variable and the
rest to the next? Then it's x = s.split('|'); a, b = x[0], x[1:].

--
Erik Max Francis && && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
It's funny when you think about it / How coincidence rules
-- Anggun


Erik Max Francis
  Reply With Quote
Old 10-19-2005, 02:50 AM   #4
bonono@gmail.com
 
Posts: n/a
Default Re: how best to split into singleton and sequence
what you wrote is the most readable to me:

just asign the first 2 element to t, l respectively and forget about
the rest. I assume that is what you want. I think perl may do it this
way.

A solution which I think looks uglier is :

t, l = s.split('|')[:2]

Randy Bush wrote:
> >>> l = []
> >>> s = 'a|b'
> >>> t, l = s.split('|')
> >>> t

> 'a'
> >>> l

> 'b'
> >>> s = 'a|b|c|d'
> >>> t, l = s.split('|')

> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> ValueError: too many values to unpack
> >>>

>
> so, i imagine what is happening is the lhs, t,l, is really
> (t, (l)), i.e. only two items.
>
> so how should i have done this readably and simply?
>
> randy




bonono@gmail.com
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46