By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,089 Members | 2,260 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,089 IT Pros & Developers. It's quick & easy.

how best to split into singleton and sequence

P: n/a
>>> 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

Oct 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Randy Bush" <ra***@psg.com> 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
Oct 18 '05 #2

P: n/a
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 && ma*@alcyone.com && 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
Oct 19 '05 #3

P: n/a
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


Oct 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.