Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Unpaking Tuple
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  18 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
sajuptpm  
View profile  
 More options Oct 6 2012, 6:09 am
Newsgroups: comp.lang.python
From: sajuptpm <sajup...@gmail.com>
Date: Sat, 6 Oct 2012 03:09:30 -0700 (PDT)
Local: Sat, Oct 6 2012 6:09 am
Subject: Unpaking Tuple
Hi,

I am using python 2.6.

I need a way to make following code working without any ValueError .

>>> a, b, c, d = (1,2,3,4)
>>> a, b, c, d = (1,2,3).

Note: Number of values in the tuple will change dynamically.

I know in python 3, you can do `a, b, c, *d = (1, 2, 3)` and then d will contain any elements that didn't fit into a,b,c.

Regards,
Saju


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris Rebert  
View profile  
 More options Oct 6 2012, 6:27 am
Newsgroups: comp.lang.python
From: Chris Rebert <c...@rebertia.com>
Date: Sat, 6 Oct 2012 03:27:52 -0700
Local: Sat, Oct 6 2012 6:27 am
Subject: Re: Unpaking Tuple

On Sat, Oct 6, 2012 at 3:09 AM, sajuptpm <sajup...@gmail.com> wrote:
> Hi,

> I am using python 2.6.

> I need a way to make following code working without any ValueError .
>>>> a, b, c, d = (1,2,3,4)
>>>> a, b, c, d = (1,2,3).

> Note: Number of values in the tuple will change dynamically.

Then you arguably want a list, not a tuple.

But at any rate:
shortfall = 4 - len(your_tuple)
your_tuple += (None,) * shortfall # assuming None is a suitable default
a, b, c, d = your_tuple

If you also need to handle the "too many items" case, use slicing:
a, b, c, d = your_tuple[:4]

Cheers,
Chris


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Roy Smith  
View profile  
 More options Oct 6 2012, 8:46 am
Newsgroups: comp.lang.python
From: Roy Smith <r...@panix.com>
Date: Sat, 06 Oct 2012 08:46:28 -0400
Local: Sat, Oct 6 2012 8:46 am
Subject: Re: Unpaking Tuple
In article <mailman.1898.1349519275.27098.python-l...@python.org>,
 Chris Rebert <c...@rebertia.com> wrote:

> But at any rate:
> shortfall = 4 - len(your_tuple)
> your_tuple += (None,) * shortfall # assuming None is a suitable default
> a, b, c, d = your_tuple

> If you also need to handle the "too many items" case, use slicing:
> a, b, c, d = your_tuple[:4]

I usually handle both of those cases at the same time:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Steven D'Aprano  
View profile  
 More options Oct 6 2012, 11:08 am
Newsgroups: comp.lang.python
From: Steven D'Aprano <steve+comp.lang.pyt...@pearwood.info>
Date: 06 Oct 2012 15:08:38 GMT
Local: Sat, Oct 6 2012 11:08 am
Subject: Re: Unpaking Tuple

On Sat, 06 Oct 2012 08:46:28 -0400, Roy Smith wrote:
> In article <mailman.1898.1349519275.27098.python-l...@python.org>,
>  Chris Rebert <c...@rebertia.com> wrote:

>> But at any rate:
>> shortfall = 4 - len(your_tuple)
>> your_tuple += (None,) * shortfall # assuming None is a suitable default
>> a, b, c, d = your_tuple

>> If you also need to handle the "too many items" case, use slicing: a,
>> b, c, d = your_tuple[:4]

> I usually handle both of those cases at the same time:

>>>> a, b, c, d = (my_tuple + (None,) * 4)[:4]

While that's fine for small tuples, if somebody wanted to mess with you,
and passed (say) a million-item tuple, that would unnecessarily copy all
million items before throwing all but four away.

For the sake of a two-liner instead of a one-liner, you can avoid such
nasty surprises:

my_tuple = my_tuple[:4]
a,b,c,d = my_tuple if len(my_tuple) == 4 else (my_tuple + (None,)*4)[:4]

A similar solution:

a,b,c,d = (my_tuple[:4] + (None, None, None, None))[:4]

Here's a dumb one-liner, good for making people laugh at you:

a,b,c,d = (x for i,x in enumerate(my_tuple + (None,)*4) if i < 4)

and an obfuscated solution:

from itertools import izip_longest as izip
a,b,c,d = (None if x == (None,None) else x[0][1] for x in izip(zip
('izip', my_tuple), (None for izip in 'izip')))

But in my opinion, the best solution of all is a three-liner:

if len(my_tuple) < 4:
    my_tuple += (None,)*(4 - len(my_tuple))
a,b,c,d = my_tuple[4:]

--
Steven


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
woooee  
View profile  
 More options Oct 7 2012, 1:58 pm
Newsgroups: comp.lang.python
From: woooee <woo...@gmail.com>
Date: Sun, 7 Oct 2012 10:58:18 -0700 (PDT)
Local: Sun, Oct 7 2012 1:58 pm
Subject: Re: Unpaking Tuple
On Oct 6, 3:09 am, sajuptpm <sajup...@gmail.com> wrote:

> I need a way to make following code working without any ValueError .

> >>> a, b, c, d = (1,2,3,4)
> >>> a, b, c, d = (1,2,3).

Why is it necessary to unpack the tuple into arbitrary variables.
a_tuple=(1,2,3)
for v in a_tuple:
    print v

for ctr in range(len(a_tuple)):
    print a_tuple[ctr]


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Terry Reedy  
View profile  
 More options Oct 7 2012, 4:03 pm
Newsgroups: comp.lang.python
From: Terry Reedy <tjre...@udel.edu>
Date: Sun, 07 Oct 2012 16:03:20 -0400
Local: Sun, Oct 7 2012 4:03 pm
Subject: Re: Unpaking Tuple
On 10/7/2012 1:58 PM, woooee wrote:

> On Oct 6, 3:09 am, sajuptpm <sajup...@gmail.com> wrote:
>> I need a way to make following code working without any ValueError .

>>>>> a, b, c, d = (1,2,3,4)
>>>>> a, b, c, d = (1,2,3)

You cannot 'make' buggy code work -- except by changing it.
 >>> a, b, c, *d = (1,2,3)
 >>> d
[]

> Why is it necessary to unpack the tuple into arbitrary variables.

It is not necessary.

> a_tuple=(1,2,3)
> for v in a_tuple:
>      print v

This is often the right way to access any iterable.

> for ctr in range(len(a_tuple)):
>      print a_tuple[ctr]

This is seldom the right way. See the example below.

Unpacking is for when you have known-length iterables. For instance,
enumerate produces pairs.

 >>> for i, v in enumerate('abc'):
   print('Item {} is {}.'.format(i, v))

Item 0 is a.
Item 1 is b.
Item 2 is c.

--
Terry Jan Reedy


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Thomas Bach  
View profile  
 More options Oct 8 2012, 5:47 pm
Newsgroups: comp.lang.python
From: Thomas Bach <thb...@students.uni-mainz.de>
Date: Mon, 8 Oct 2012 23:45:58 +0200
Local: Mon, Oct 8 2012 5:45 pm
Subject: Re: Unpaking Tuple
Hi there,

On Sat, Oct 06, 2012 at 03:08:38PM +0000, Steven D'Aprano wrote:

> my_tuple = my_tuple[:4]
> a,b,c,d = my_tuple if len(my_tuple) == 4 else (my_tuple + (None,)*4)[:4]

Are you sure this works as you expect? I just stumbled over the following:

$ python
Python 3.2.3 (default, Jun 25 2012, 23:10:56)
[GCC 4.7.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.

>>> split = ['foo', 'bar']
>>> head, tail = split if len(split) == 2 else split[0], None
>>> head
['foo', 'bar']
>>> tail

I don't get it! Could someone help me, please? Why is head not 'foo'
and tail not 'bar'?

Regards,
        Thomas


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Prasad, Ramit  
View profile  
 More options Oct 8 2012, 6:44 pm
Newsgroups: comp.lang.python
From: "Prasad, Ramit" <ramit.pra...@jpmorgan.com>
Date: Mon, 8 Oct 2012 22:21:26 +0000
Local: Mon, Oct 8 2012 6:21 pm
Subject: RE: Unpaking Tuple

I think you just need to wrap the else in parenthesis so the
else clause is treated as a tuple. Without the parenthesis
I believe it is grouping the code like this.

head, tail = (split if len(split) == 2 else split[0] ), None

You want:
head, tail = split if len(split) == 2 else (split[0], None )

Ramit
This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Bob Martin  
View profile  
 More options Oct 9 2012, 2:07 am
Newsgroups: comp.lang.python
From: Bob Martin <bob.mar...@excite.com>
Date: Tue, 09 Oct 2012 07:07:32 BST
Local: Tues, Oct 9 2012 2:07 am
Subject: Re: RE: Unpaking Tuple
in 682592 20121008 232126 "Prasad, Ramit" <ramit.pra...@jpmorgan.com> wrote:

How does one unpack this post?  ;-)

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dave Angel  
View profile  
 More options Oct 9 2012, 2:29 am
Newsgroups: comp.lang.python
From: Dave Angel <d...@davea.name>
Date: Tue, 09 Oct 2012 02:29:09 -0400
Local: Tues, Oct 9 2012 2:29 am
Subject: Re: Unpaking Tuple
On 10/09/2012 02:07 AM, Bob Martin wrote:

> in 682592 20121008 232126 "Prasad, Ramit" <ramit.pra...@jpmorgan.com> wrote:
>> Thomas Bach wrote:=0D=0A> Hi there,=0D=0A> =0D=0A> On Sat, Oct 06, 2012 at =
>> 03:08:38PM +0000, Steven D'Aprano wrote:=0D=0A> >=0D=0A> > my_tuple =3D my_=
>> tuple[:4]=0D=0A> > a,b,c,d =3D my_tuple if len(my_tuple) =3D=3D 4 else (my_=
>> <SNIP>
>> y and completeness of information, viruses,=0D=0Aconfidentiality, legal pri=
>> vilege, and legal entity disclaimers,=0D=0Aavailable at http://www=2Ejpmorg=
>> an=2Ecom/pages/disclosures/email=2E
> How does one unpack this post?  ;-)

Since that's not the way it arrived here, i have to ask, how do you get
these posts?  Are you subscribed to individual messages by email via
python.org?  or are you using google-groups or some other indirection?

In any reasonable mail program, you can see the source of a message.
Most of the troubles i've seen here have been caused by people trying to
send html mail to a text-based mailing list.  But in the case you quote,
the original message came here as text/plain, and well formatted.

--

DaveA


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jussi Piitulainen  
View profile  
 More options Oct 9 2012, 3:22 am
Newsgroups: comp.lang.python
From: Jussi Piitulainen <jpiit...@ling.helsinki.fi>
Date: 09 Oct 2012 10:22:26 +0300
Local: Tues, Oct 9 2012 3:22 am
Subject: Re: Unpaking Tuple

I see a carriage return rendered as ^M at the end of every line from
Prasad's messages. Other than that, they are well-formatted plain text
for me, too.

I guess Prasad's system sends \r\n instead of \n\r (the DOS line-end)
and \r\n gets interpreted as a stray \r followed by end-of-line.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "mangled messages (was: Unpaking Tuple)" by Tim Chase
Tim Chase  
View profile  
 More options Oct 9 2012, 7:04 am
Newsgroups: comp.lang.python
From: Tim Chase <python.l...@tim.thechases.com>
Date: Tue, 09 Oct 2012 05:48:58 -0500
Local: Tues, Oct 9 2012 6:48 am
Subject: Re: mangled messages (was: Unpaking Tuple)
On 10/09/12 02:22, Jussi Piitulainen wrote:

>>> in 682592 20121008 232126 "Prasad, Ramit" wrote:
> [snip mess]
>>> How does one unpack this post?  ;-)

>> Since that's not the way it arrived here, i have to ask, how do you
>> get these posts?

> I see a carriage return rendered as ^M at the end of every line from
> Prasad's messages. Other than that, they are well-formatted plain text
> for me, too.

> I guess Prasad's system sends \r\n instead of \n\r (the DOS line-end)
> and \r\n gets interpreted as a stray \r followed by end-of-line.

Prasad's system is correctly sending the "right" order (DOS
line-ends are CR+LF = \r\n, not the other way around).  However, it
might be that there is no CR+LF on the last line, or that one line
is missing the CR, so your viewer heuristic (vim does this) thinks
it has Unix NL-only line-endings and shows the ^M on all the lines
that have the CR.  All for one stray line without.

Prasad's email came through cleanly here (gmane + Thunderbird).

-tkc


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jussi Piitulainen  
View profile  
 More options Oct 9 2012, 8:05 am
Newsgroups: comp.lang.python
From: Jussi Piitulainen <jpiit...@ling.helsinki.fi>
Date: 09 Oct 2012 15:05:42 +0300
Local: Tues, Oct 9 2012 8:05 am
Subject: Re: mangled messages (was: Unpaking Tuple)

You are right. I managed to confuse myself about the order of the two
characters while staring on a source that says the opposite of what I
said (http://en.wikipedia.org/wiki/Newline).

Doubly sorry about the noise (being both off-topic and incorrect).

> However, it might be that there is no CR+LF on the last line, or
> that one line is missing the CR, so your viewer heuristic (vim does
> this) thinks it has Unix NL-only line-endings and shows the ^M on
> all the lines that have the CR.  All for one stray line without.

That doesn't sound robust. The problem is still quite rare for me.

> Prasad's email came through cleanly here (gmane + Thunderbird).

I'm on Gnus in Emacs, probably a few years out of date.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "Unpaking Tuple" by Grant Edwards
Grant Edwards  
View profile  
 More options Oct 9 2012, 10:11 am
Newsgroups: comp.lang.python
From: Grant Edwards <inva...@invalid.invalid>
Date: Tue, 9 Oct 2012 14:11:26 +0000 (UTC)
Local: Tues, Oct 9 2012 10:11 am
Subject: Re: Unpaking Tuple
On 2012-10-09, Bob Martin <bob.mar...@excite.com> wrote:

> in 682592 20121008 232126 "Prasad, Ramit" <ramit.pra...@jpmorgan.com> wrote:
>>Thomas Bach wrote:=0D=0A> Hi there,=0D=0A> =0D=0A> On Sat, Oct 06, 2012 at =
>>03:08:38PM +0000, Steven D'Aprano wrote:=0D=0A> >=0D=0A> > my_tuple =3D my_=
>>tuple[:4]=0D=0A> > a,b,c,d =3D my_tuple if len(my_tuple) =3D=3D 4 else (my_=

> How does one unpack this post?  ;-)

Yea, my newsreader doesn't like those posts either -- though they're
not as bad as what yours displays. Mine just shows "^M" strings all
at the end of every line.

--
Grant Edwards               grant.b.edwards        Yow! BARBARA STANWYCK makes
                                  at               me nervous!!
                              gmail.com            


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "mangled messages" by Tim Chase
Tim Chase  
View profile  
 More options Oct 9 2012, 10:25 am
Newsgroups: comp.lang.python
From: Tim Chase <python.l...@tim.thechases.com>
Date: Tue, 09 Oct 2012 09:26:15 -0500
Local: Tues, Oct 9 2012 10:26 am
Subject: Re: mangled messages
On 10/09/12 07:05, Jussi Piitulainen wrote:

> Tim Chase writes:
>> However, it might be that there is no CR+LF on the last line,
>> or that one line is missing the CR, so your viewer heuristic
>> (vim does this) thinks it has Unix NL-only line-endings and
>> shows the ^M on all the lines that have the CR.  All for one
>> stray line without.

> That doesn't sound robust. The problem is still quite rare for
> me.

Vim's heuristic is that, if *all* the lines end in CR+LF, it's a
DOS-formatted file; otherwise it's a Unix-style (LF) file with
spurious CRs in it (they just happen to come at the end of
most-but-not-all lines).  It works quite robustly, since writing the
file back out will reliably put the CRs back where they were and
leave the non-CR'ed lines as they were with only LF.  Vim makes it
pretty easy to remove the spurious CRs and then change the
file-format from Unix to DOS line-endings and write it out if that's
what you want[1].

-tkc

[1]
:%s/\r$
:set ff=dos
:w

which (1) removes the spurious/inconsistent CRs, (2) tells vim that
newlines should be written as CR+LF when writing and (3) writes the
file back out to disk.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "Unpaking Tuple" by Prasad, Ramit
Prasad, Ramit  
View profile  
 More options Oct 9 2012, 12:40 pm
Newsgroups: comp.lang.python
From: "Prasad, Ramit" <ramit.pra...@jpmorgan.com>
Date: Tue, 9 Oct 2012 16:40:17 +0000
Local: Tues, Oct 9 2012 12:40 pm
Subject: RE: RE: Unpaking Tuple
Bob Martin wrote

Hmm, I am not sure why that happened. For reference:
http://mail.python.org/pipermail/python-list/2012-October/632603.html
This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Miles  
View profile  
 More options Nov 18 2012, 8:14 pm
Newsgroups: comp.lang.python
From: Robert Miles <robertmi...@teranews.com>
Date: Sun, 18 Nov 2012 19:14:08 -0600
Local: Sun, Nov 18 2012 8:14 pm
Subject: Re: Unpaking Tuple
On 10/9/2012 1:07 AM, Bob Martin wrote:

There are a number of programs for converting ends of lines between
Linux format, Windows format, and Mac formats.  You could try running
all of those programs your operating system provides on that text,
then checking which one of them gives the most readable results.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Hans Mulder  
View profile  
 More options Nov 18 2012, 8:56 pm
Newsgroups: comp.lang.python
From: Hans Mulder <han...@xs4all.nl>
Date: Mon, 19 Nov 2012 02:56:29 +0100
Local: Sun, Nov 18 2012 8:56 pm
Subject: Re: Unpaking Tuple
On 9/10/12 08:07:32, Bob Martin wrote:

How about:

    print re.sub('^>* ', '', this_post, flags=re.M).decode('quopri')

Hope this helps,

-- HansM


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »