Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #30881 > unrolled thread
| Started by | sajuptpm <sajuptpm@gmail.com> |
|---|---|
| First post | 2012-10-06 03:09 -0700 |
| Last post | 2012-10-07 16:03 -0400 |
| Articles | 18 — 15 participants |
Back to article view | Back to comp.lang.python
Unpaking Tuple sajuptpm <sajuptpm@gmail.com> - 2012-10-06 03:09 -0700
Re: Unpaking Tuple Chris Rebert <clp2@rebertia.com> - 2012-10-06 03:27 -0700
Re: Unpaking Tuple Roy Smith <roy@panix.com> - 2012-10-06 08:46 -0400
Re: Unpaking Tuple Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-10-06 15:08 +0000
Re: Unpaking Tuple Thomas Bach <thbach@students.uni-mainz.de> - 2012-10-08 23:45 +0200
RE: Unpaking Tuple "Prasad, Ramit" <ramit.prasad@jpmorgan.com> - 2012-10-08 22:21 +0000
Re: RE: Unpaking Tuple Bob Martin <bob.martin@excite.com> - 2012-10-09 07:07 +0100
Re: Unpaking Tuple Dave Angel <d@davea.name> - 2012-10-09 02:29 -0400
Re: Unpaking Tuple Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2012-10-09 10:22 +0300
Re: mangled messages (was: Unpaking Tuple) Tim Chase <python.list@tim.thechases.com> - 2012-10-09 05:48 -0500
Re: mangled messages (was: Unpaking Tuple) Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2012-10-09 15:05 +0300
Re: mangled messages Tim Chase <python.list@tim.thechases.com> - 2012-10-09 09:26 -0500
Re: Unpaking Tuple Grant Edwards <invalid@invalid.invalid> - 2012-10-09 14:11 +0000
RE: RE: Unpaking Tuple "Prasad, Ramit" <ramit.prasad@jpmorgan.com> - 2012-10-09 16:40 +0000
Re: Unpaking Tuple Robert Miles <robertmiles@teranews.com> - 2012-11-18 19:14 -0600
Re: Unpaking Tuple Hans Mulder <hansmu@xs4all.nl> - 2012-11-19 02:56 +0100
Re: Unpaking Tuple woooee <woooee@gmail.com> - 2012-10-07 10:58 -0700
Re: Unpaking Tuple Terry Reedy <tjreedy@udel.edu> - 2012-10-07 16:03 -0400
| From | sajuptpm <sajuptpm@gmail.com> |
|---|---|
| Date | 2012-10-06 03:09 -0700 |
| Subject | Unpaking Tuple |
| Message-ID | <801f0e2c-7d1d-4e91-bec5-78c5e53a70ec@googlegroups.com> |
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
[toc] | [next] | [standalone]
| From | Chris Rebert <clp2@rebertia.com> |
|---|---|
| Date | 2012-10-06 03:27 -0700 |
| Message-ID | <mailman.1898.1349519275.27098.python-list@python.org> |
| In reply to | #30881 |
On Sat, Oct 6, 2012 at 3:09 AM, sajuptpm <sajuptpm@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
[toc] | [prev] | [next] | [standalone]
| From | Roy Smith <roy@panix.com> |
|---|---|
| Date | 2012-10-06 08:46 -0400 |
| Message-ID | <roy-289A2F.08462806102012@news.panix.com> |
| In reply to | #30883 |
In article <mailman.1898.1349519275.27098.python-list@python.org>, Chris Rebert <clp2@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]
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2012-10-06 15:08 +0000 |
| Message-ID | <50704975$0$29978$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #30887 |
On Sat, 06 Oct 2012 08:46:28 -0400, Roy Smith wrote:
> In article <mailman.1898.1349519275.27098.python-list@python.org>,
> Chris Rebert <clp2@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
[toc] | [prev] | [next] | [standalone]
| From | Thomas Bach <thbach@students.uni-mainz.de> |
|---|---|
| Date | 2012-10-08 23:45 +0200 |
| Message-ID | <mailman.1970.1349732829.27098.python-list@python.org> |
| In reply to | #30891 |
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
[toc] | [prev] | [next] | [standalone]
| From | "Prasad, Ramit" <ramit.prasad@jpmorgan.com> |
|---|---|
| Date | 2012-10-08 22:21 +0000 |
| Message-ID | <mailman.1973.1349736270.27098.python-list@python.org> |
| In reply to | #30891 |
Thomas Bach wrote: > 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 > -- 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.
[toc] | [prev] | [next] | [standalone]
| From | Bob Martin <bob.martin@excite.com> |
|---|---|
| Date | 2012-10-09 07:07 +0100 |
| Message-ID | <adht95F2luqU1@mid.individual.net> |
| In reply to | #30980 |
in 682592 20121008 232126 "Prasad, Ramit" <ramit.prasad@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_= >tuple + (None,)*4)[:4]=0D=0A> >=0D=0A> =0D=0A> Are you sure this works as y= >ou expect? I just stumbled over the following:=0D=0A> =0D=0A> $ python=0D= >=0A> Python 3=2E2=2E3 (default, Jun 25 2012, 23:10:56)=0D=0A> [GCC 4=2E7=2E= >1] on linux2=0D=0A> Type "help", "copyright", "credits" or "license" for mo= >re information=2E=0D=0A> >>> split =3D ['foo', 'bar']=0D=0A> >>> head, tail= >=3D split if len(split) =3D=3D 2 else split[0], None=0D=0A> >>> head=0D=0A= >> ['foo', 'bar']=0D=0A> >>> tail=0D=0A> >>>=0D=0A> =0D=0A> I don't get it! = >Could someone help me, please? Why is head not 'foo'=0D=0A> and tail not 'b= >ar'?=0D=0A> =0D=0A> Regards,=0D=0A> Thomas=0D=0A> --=0D=0A=0D=0AI think yo= >u just need to wrap the else in parenthesis so the=0D=0Aelse clause is trea= >ted as a tuple=2E Without the parenthesis =0D=0AI believe it is grouping th= >e code like this=2E=0D=0A=0D=0Ahead, tail =3D (split if len(split) =3D=3D 2= >else split[0] ), None=0D=0A=0D=0AYou want:=0D=0Ahead, tail =3D split if le= >n(split) =3D=3D 2 else (split[0], None )=0D=0A=0D=0A=0D=0ARamit=0D=0AThis e= >mail is confidential and subject to important disclaimers and=0D=0Aconditio= >ns including on offers for the purchase or sale of=0D=0Asecurities, accurac= >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? ;-)
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <d@davea.name> |
|---|---|
| Date | 2012-10-09 02:29 -0400 |
| Message-ID | <mailman.1982.1349764176.27098.python-list@python.org> |
| In reply to | #30997 |
On 10/09/2012 02:07 AM, Bob Martin wrote: > in 682592 20121008 232126 "Prasad, Ramit" <ramit.prasad@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
[toc] | [prev] | [next] | [standalone]
| From | Jussi Piitulainen <jpiitula@ling.helsinki.fi> |
|---|---|
| Date | 2012-10-09 10:22 +0300 |
| Message-ID | <qotr4p8cdot.fsf@ruuvi.it.helsinki.fi> |
| In reply to | #30998 |
Dave Angel writes: > On 10/09/2012 02:07 AM, Bob Martin 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? 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. 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.
[toc] | [prev] | [next] | [standalone]
| From | Tim Chase <python.list@tim.thechases.com> |
|---|---|
| Date | 2012-10-09 05:48 -0500 |
| Subject | Re: mangled messages (was: Unpaking Tuple) |
| Message-ID | <mailman.1987.1349780698.27098.python-list@python.org> |
| In reply to | #31000 |
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
[toc] | [prev] | [next] | [standalone]
| From | Jussi Piitulainen <jpiitula@ling.helsinki.fi> |
|---|---|
| Date | 2012-10-09 15:05 +0300 |
| Subject | Re: mangled messages (was: Unpaking Tuple) |
| Message-ID | <qotpq4r7svd.fsf@ruuvi.it.helsinki.fi> |
| In reply to | #31003 |
Tim Chase writes: > 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). 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.
[toc] | [prev] | [next] | [standalone]
| From | Tim Chase <python.list@tim.thechases.com> |
|---|---|
| Date | 2012-10-09 09:26 -0500 |
| Subject | Re: mangled messages |
| Message-ID | <mailman.2000.1349792713.27098.python-list@python.org> |
| In reply to | #31011 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Grant Edwards <invalid@invalid.invalid> |
|---|---|
| Date | 2012-10-09 14:11 +0000 |
| Message-ID | <k51bae$4qb$1@reader1.panix.com> |
| In reply to | #30997 |
On 2012-10-09, Bob Martin <bob.martin@excite.com> wrote:
> in 682592 20121008 232126 "Prasad, Ramit" <ramit.prasad@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
[toc] | [prev] | [next] | [standalone]
| From | "Prasad, Ramit" <ramit.prasad@jpmorgan.com> |
|---|---|
| Date | 2012-10-09 16:40 +0000 |
| Message-ID | <mailman.2010.1349800838.27098.python-list@python.org> |
| In reply to | #30997 |
Bob Martin wrote > in 682592 20121008 232126 "Prasad, Ramit" <ramit.prasad@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_= > >tuple + (None,)*4)[:4]=0D=0A> >=0D=0A> =0D=0A> Are you sure this works as y= > >ou expect? I just stumbled over the following:=0D=0A> =0D=0A> $ python=0D= > >=0A> Python 3=2E2=2E3 (default, Jun 25 2012, 23:10:56)=0D=0A> [GCC 4=2E7=2E= > >1] on linux2=0D=0A> Type "help", "copyright", "credits" or "license" for mo= > >re information=2E=0D=0A> >>> split =3D ['foo', 'bar']=0D=0A> >>> head, tail= > >=3D split if len(split) =3D=3D 2 else split[0], None=0D=0A> >>> head=0D=0A= > >> ['foo', 'bar']=0D=0A> >>> tail=0D=0A> >>>=0D=0A> =0D=0A> I don't get it! = > >Could someone help me, please? Why is head not 'foo'=0D=0A> and tail not 'b= > >ar'?=0D=0A> =0D=0A> Regards,=0D=0A> Thomas=0D=0A> --=0D=0A=0D=0AI think yo= > >u just need to wrap the else in parenthesis so the=0D=0Aelse clause is trea= > >ted as a tuple=2E Without the parenthesis =0D=0AI believe it is grouping th= > >e code like this=2E=0D=0A=0D=0Ahead, tail =3D (split if len(split) =3D=3D 2= > >else split[0] ), None=0D=0A=0D=0AYou want:=0D=0Ahead, tail =3D split if le= > >n(split) =3D=3D 2 else (split[0], None )=0D=0A=0D=0A=0D=0ARamit=0D=0AThis e= > >mail is confidential and subject to important disclaimers and=0D=0Aconditio= > >ns including on offers for the purchase or sale of=0D=0Asecurities, accurac= > >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? ;-) > -- 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.
[toc] | [prev] | [next] | [standalone]
| From | Robert Miles <robertmiles@teranews.com> |
|---|---|
| Date | 2012-11-18 19:14 -0600 |
| Message-ID | <1Kfqs.6517$fF5.3545@newsfe01.iad> |
| In reply to | #30997 |
On 10/9/2012 1:07 AM, Bob Martin wrote: > in 682592 20121008 232126 "Prasad, Ramit" <ramit.prasad@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_= >> tuple + (None,)*4)[:4]=0D=0A> >=0D=0A> =0D=0A> Are you sure this works as y= >> ou expect? I just stumbled over the following:=0D=0A> =0D=0A> $ python=0D= >> =0A> Python 3=2E2=2E3 (default, Jun 25 2012, 23:10:56)=0D=0A> [GCC 4=2E7=2E= >> 1] on linux2=0D=0A> Type "help", "copyright", "credits" or "license" for mo= >> re information=2E=0D=0A> >>> split =3D ['foo', 'bar']=0D=0A> >>> head, tail= >> =3D split if len(split) =3D=3D 2 else split[0], None=0D=0A> >>> head=0D=0A= >>> ['foo', 'bar']=0D=0A> >>> tail=0D=0A> >>>=0D=0A> =0D=0A> I don't get it! = >> Could someone help me, please? Why is head not 'foo'=0D=0A> and tail not 'b= >> ar'?=0D=0A> =0D=0A> Regards,=0D=0A> Thomas=0D=0A> --=0D=0A=0D=0AI think yo= >> u just need to wrap the else in parenthesis so the=0D=0Aelse clause is trea= >> ted as a tuple=2E Without the parenthesis =0D=0AI believe it is grouping th= >> e code like this=2E=0D=0A=0D=0Ahead, tail =3D (split if len(split) =3D=3D 2= >> else split[0] ), None=0D=0A=0D=0AYou want:=0D=0Ahead, tail =3D split if le= >> n(split) =3D=3D 2 else (split[0], None )=0D=0A=0D=0A=0D=0ARamit=0D=0AThis e= >> mail is confidential and subject to important disclaimers and=0D=0Aconditio= >> ns including on offers for the purchase or sale of=0D=0Asecurities, accurac= >> 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? ;-) 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.
[toc] | [prev] | [next] | [standalone]
| From | Hans Mulder <hansmu@xs4all.nl> |
|---|---|
| Date | 2012-11-19 02:56 +0100 |
| Message-ID | <50a991cd$0$6861$e4fe514c@news2.news.xs4all.nl> |
| In reply to | #30997 |
On 9/10/12 08:07:32, Bob Martin wrote:
> in 682592 20121008 232126 "Prasad, Ramit" <ramit.prasad@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_=
>> tuple + (None,)*4)[:4]=0D=0A> >=0D=0A> =0D=0A> Are you sure this works as y=
>> ou expect? I just stumbled over the following:=0D=0A> =0D=0A> $ python=0D=
>> =0A> Python 3=2E2=2E3 (default, Jun 25 2012, 23:10:56)=0D=0A> [GCC 4=2E7=2E=
>> 1] on linux2=0D=0A> Type "help", "copyright", "credits" or "license" for mo=
>> re information=2E=0D=0A> >>> split =3D ['foo', 'bar']=0D=0A> >>> head, tail=
>> =3D split if len(split) =3D=3D 2 else split[0], None=0D=0A> >>> head=0D=0A=
>>> ['foo', 'bar']=0D=0A> >>> tail=0D=0A> >>>=0D=0A> =0D=0A> I don't get it! =
>> Could someone help me, please? Why is head not 'foo'=0D=0A> and tail not 'b=
>> ar'?=0D=0A> =0D=0A> Regards,=0D=0A> Thomas=0D=0A> --=0D=0A=0D=0AI think yo=
>> u just need to wrap the else in parenthesis so the=0D=0Aelse clause is trea=
>> ted as a tuple=2E Without the parenthesis =0D=0AI believe it is grouping th=
>> e code like this=2E=0D=0A=0D=0Ahead, tail =3D (split if len(split) =3D=3D 2=
>> else split[0] ), None=0D=0A=0D=0AYou want:=0D=0Ahead, tail =3D split if le=
>> n(split) =3D=3D 2 else (split[0], None )=0D=0A=0D=0A=0D=0ARamit=0D=0AThis e=
>> mail is confidential and subject to important disclaimers and=0D=0Aconditio=
>> ns including on offers for the purchase or sale of=0D=0Asecurities, accurac=
>> 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? ;-)
How about:
print re.sub('^>* ', '', this_post, flags=re.M).decode('quopri')
Hope this helps,
-- HansM
[toc] | [prev] | [next] | [standalone]
| From | woooee <woooee@gmail.com> |
|---|---|
| Date | 2012-10-07 10:58 -0700 |
| Message-ID | <69987244-3aa8-441b-83e8-2275e75f2b83@ph9g2000pbb.googlegroups.com> |
| In reply to | #30881 |
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]
[toc] | [prev] | [next] | [standalone]
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2012-10-07 16:03 -0400 |
| Message-ID | <mailman.1934.1349640228.27098.python-list@python.org> |
| In reply to | #30929 |
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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web