473,509 Members | 2,963 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Oct 18 '05 #1
3 1373
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3256
by: oliver | last post by:
hi there i'm experimanting with imaplib and came across stringts like (\HasNoChildren) "." "INBOX.Sent Items" in which the quotes are part of the string. now i try to convert this into a...
12
5858
by: David WOO | last post by:
Hi, I am a newbie on C++, I need to define some global variables which should be accessible to most classes. In the mean time, I don't won't the global variables be modified freely at most of...
16
6687
by: cppaddict | last post by:
Hi, In this tutorial on singleton class in C++ (http://gethelp.devx.com/techtips/cpp_pro/10min/10min0200.asp) the author gives two implementations of a simple singleton class, claiming that...
10
1454
by: A_StClaire_ | last post by:
hi, I have a singleton Evaluation class that I'm calling repeatedly in one sequence. would someone plz have a look at the code below and tell me if one instance of the singleton can ever...
10
2168
by: Robert R. | last post by:
Hello, i would like to write a piece of code to help me to align some sequence of words and suggest me the ordered common subwords of them s0 = "this is an example of a thing i would like to...
2
2213
by: rn5a | last post by:
A Form has a select list which is populated from a MS-Access database table. The DB table from where the select list is populated has 2 columns - CountryID & CountryName. When the Form is posted,...
3
18224
weaknessforcats
by: weaknessforcats | last post by:
Design Pattern: The Singleton Overview Use the Singleton Design Pattern when you want to have only one instance of a class. This single instance must have a single global point of access. That...
7
3481
by: Johny | last post by:
I have a string of a variable length and I need to split the string in strings of 6 characters . But if the 6th character is not space then I must split the string at possition before the 6th...
8
2748
by: Slaunger | last post by:
Hi all, I am a Python novice, and I have run into a problem in a project I am working on, which boils down to identifying the patterns in a sequence of integers, for example ..... 1 6 6 1 6 6...
0
7234
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7412
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7505
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5652
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5060
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4730
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3216
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3203
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
441
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.