>>> 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 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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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...
|
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...
|
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...
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
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,...
|
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...
| |
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...
|
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,...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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...
| |