how best to split into singleton and sequence

R

Randy Bush

l = []Traceback (most recent call last):

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
 
G

George Sakkis

Randy Bush said:
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):

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?

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

George
 
E

Erik Max Francis

Randy said:
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:].
 
B

bonono

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 said:
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):

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top