473,666 Members | 2,088 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 1376
"Randy Bush" <ra***@psg.co m> 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
3266
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 list. assume the string is in the variable f, then i tried f.split() but i end up with
12
5870
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 these classes. I know there is a pattern called singleton can more or less do such a trick. I am wondering is this the best way to do it (regarding the convenience and safety), as this is such a fundamental thing, I believe most of you have a say...
16
6721
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 only the first is safe for multi-threaded appliactions. I want to know why this so. The class is as follows:
10
1472
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 "clash" with another? I'm asking because I'm getting unexpected results. I've looked for the problem the past several days and can't attribute the anomalies to logic errors.
10
2182
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 have".split() s1 = "another example of something else i would like to have".split() s2 = 'and this is another " example " but of something ; now i would still like to have'.split() ....
2
2224
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, the option selected in the select list is inserted in another DB table wherein the column in which the value of the selected option will be populated accepts only integers. Actually there are 7-8 select lists & the values of all the selected...
3
18240
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 is, regardless of where the object is hidden, everyone needs access to it. The global point of access is the object's Instance() method. Individual users need to be prevented from creating their own instances of the Singleton.
7
3486
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 character. For example: if the main string S is S='abcde fghc ijkl mnop'
8
2759
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 1 6 6 1 6 6 1 6 6 1 9 3 3 0 3 3 0 3 3 0 3 3 0 10 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1 9 3 3 0 3 3 0 3 3 0 3 3 0 10 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1 9 3 3 0 3 3 0 3 3 0 3 3 0 10 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1 9 3 3 0 3 3 0...
0
8876
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8784
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8642
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6198
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4198
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4371
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2774
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1777
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.