Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #66795 > unrolled thread

Remove comma from tuples in python.

Started byJaydeep Patil <patil.jay2009@gmail.com>
First post2014-02-20 22:49 -0800
Last post2014-02-21 14:02 +0000
Articles 14 — 13 participants

Back to article view | Back to comp.lang.python


Contents

  Remove comma from tuples in python. Jaydeep Patil <patil.jay2009@gmail.com> - 2014-02-20 22:49 -0800
    Re: Remove comma from tuples in python. Mircescu Andrei <mircescu.andrei@gmail.com> - 2014-02-20 23:02 -0800
      Re: Remove comma from tuples in python. Stephane Wirtel <stephane@wirtel.be> - 2014-02-21 08:11 +0100
      Re: Remove comma from tuples in python. Bernd Nawothnig <Bernd.Nawothnig@t-online.de> - 2014-02-21 08:20 +0100
    Re: Remove comma from tuples in python. Gary Herron <gary.herron@islandtraining.com> - 2014-02-21 00:27 -0800
      Re: Remove comma from tuples in python. Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2014-02-21 11:13 +0200
        Re: Remove comma from tuples in python. Alister <alister.ware@ntlworld.com> - 2014-02-21 09:29 +0000
          Re: Remove comma from tuples in python. Tim Chase <python.list@tim.thechases.com> - 2014-02-21 08:00 -0600
          Re: Remove comma from tuples in python. Peter Otten <__peter__@web.de> - 2014-02-21 15:14 +0100
            Re: Remove comma from tuples in python. Roy Smith <roy@panix.com> - 2014-02-21 09:32 -0500
              Re: Remove comma from tuples in python. Travis Griggs <travisgriggs@gmail.com> - 2014-02-21 09:48 -0800
              Re: Remove comma from tuples in python. Tim Chase <python.list@tim.thechases.com> - 2014-02-21 12:07 -0600
              Re: Remove comma from tuples in python. Cameron Simpson <cs@zip.com.au> - 2014-02-24 12:19 +1100
    Re: Remove comma from tuples in python. Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-02-21 14:02 +0000

#66795 — Remove comma from tuples in python.

FromJaydeep Patil <patil.jay2009@gmail.com>
Date2014-02-20 22:49 -0800
SubjectRemove comma from tuples in python.
Message-ID<8ca893cc-c010-486b-ace3-070ff7ffdac8@googlegroups.com>
I am getting below tuple from excel.
How should i remove extra commas in each tuple to make it easy for operations.

tuples is:
seriesxlist1 = ((0.0), (0.01), (0.02), (0.03), (0.04), (0.05), (0.06), (0.07), (0.08), (0.09), (0.1), (0.11))



please suggest me solution.



Regards
jay

[toc] | [next] | [standalone]


#66800

FromMircescu Andrei <mircescu.andrei@gmail.com>
Date2014-02-20 23:02 -0800
Message-ID<38442805-6700-4086-9e55-acaa74a42979@googlegroups.com>
In reply to#66795
vineri, 21 februarie 2014, 08:49:01 UTC+2, Jaydeep Patil a scris:
> I am getting below tuple from excel.
> 
> How should i remove extra commas in each tuple to make it easy for operations.
> 
> 
> 
> tuples is:
> 
> seriesxlist1 = ((0.0), (0.01), (0.02), (0.03), (0.04), (0.05), (0.06), (0.07), (0.08), (0.09), (0.1), (0.11))
> 
> 
> 
> 
> 
> 
> 
> please suggest me solution.
> 
> 
> 
> 
> 
> 
> 
> Regards
> 
> jay

i think you have a tuple of tuples there. a tuple of 12 tuples.

you need to parse each one and represent it as you wish

[toc] | [prev] | [next] | [standalone]


#66804

FromStephane Wirtel <stephane@wirtel.be>
Date2014-02-21 08:11 +0100
Message-ID<mailman.7213.1392967197.18130.python-list@python.org>
In reply to#66800
This is just a tuple of integers and not a tuple of tuples of integers, the parentheses around the number is just there for the evaluation.



> On 21 févr. 2014, at 08:02 AM, Mircescu Andrei <mircescu.andrei@gmail.com> wrote:
> 
> vineri, 21 februarie 2014, 08:49:01 UTC+2, Jaydeep Patil a scris:
>> I am getting below tuple from excel.
>> 
>> How should i remove extra commas in each tuple to make it easy for operations.
>> 
>> 
>> 
>> tuples is:
>> 
>> seriesxlist1 = ((0.0), (0.01), (0.02), (0.03), (0.04), (0.05), (0.06), (0.07), (0.08), (0.09), (0.1), (0.11))
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>> please suggest me solution.
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>> Regards
>> 
>> jay
> 
> i think you have a tuple of tuples there. a tuple of 12 tuples.
> 
> you need to parse each one and represent it as you wish
> -- 
> https://mail.python.org/mailman/listinfo/python-list

[toc] | [prev] | [next] | [standalone]


#66805

FromBernd Nawothnig <Bernd.Nawothnig@t-online.de>
Date2014-02-21 08:20 +0100
Message-ID<vabjta-r5h.ln1@bernd.nawothnig.dialin.t-online.de>
In reply to#66800
On 2014-02-21, Mircescu Andrei wrote:
> vineri, 21 februarie 2014, 08:49:01 UTC+2, Jaydeep Patil a scris:
>> I am getting below tuple from excel.
>> 
>> How should i remove extra commas in each tuple to make it easy for operations.
>> 
>> 
>> 
>> tuples is:
>> 
>> seriesxlist1 = ((0.0), (0.01), (0.02), (0.03), (0.04), (0.05), (0.06), (0.07), (0.08), (0.09), (0.1), (0.11))
>
> i think you have a tuple of tuples there. a tuple of 12 tuples.

No it isn't:

#v+
>>> a = ((0.0), (0.01), (0.02), (0.03), (0.04), (0.05), (0.06), (0.07), (0.08), (0.09), (0.1), (0.11))
>>> a
(0.0, 0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 0.1, 0.11)
#v-

The comma makes a tuple, not the parenthesis alone:


#v+
>>> a = ((0.0,), (0.01,), (0.02,), (0.03,), (0.04,), (0.05,), (0.06,), (0.07,), (0.08,), (0.09,), (0.1,), (0.11,))
>>> a
((0.0,), (0.01,), (0.02,), (0.03,), (0.04,), (0.05,), (0.06,), (0.07,), (0.08,), (0.09,), (0.1,), (0.11,))
>>> 
#v-




Bernd

-- 
no time toulouse

[toc] | [prev] | [next] | [standalone]


#66813

FromGary Herron <gary.herron@islandtraining.com>
Date2014-02-21 00:27 -0800
Message-ID<mailman.7220.1392971253.18130.python-list@python.org>
In reply to#66795
On 02/20/2014 10:49 PM, Jaydeep Patil wrote:
> I am getting below tuple from excel.
> How should i remove extra commas in each tuple to make it easy for operations.
>
> tuples is:
> seriesxlist1 = ((0.0), (0.01), (0.02), (0.03), (0.04), (0.05), (0.06), (0.07), (0.08), (0.09), (0.1), (0.11))
>
>
>
> please suggest me solution.
>
>
>
> Regards
> jay

There are no extra *commas* there.  Perhaps you mean extra 
*parentheses*?   When Python parses that line, the extra parentheses are 
used to control the evaluation (unnecessarily in this case, as it turns 
out), and won't be in the final result.

 >>> seriesxlist1 = ((0.0), (0.01), (0.02), (0.03), (0.04), (0.05), 
(0.06), (0.07), (0.08), (0.09), (0.1), (0.11))
 >>> seriesxlist1
(0.0, 0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 0.1, 0.11)

A bit of notation, because I''m not sure we are communicating well here: 
   A tuple is a Python data structure.  It has no commas or 
parentheses.   The *printing* of a Python tuple uses both for it's 
appearance on the output, but the tuple itself has no such thing.

Gary Herron

[toc] | [prev] | [next] | [standalone]


#66818

FromJussi Piitulainen <jpiitula@ling.helsinki.fi>
Date2014-02-21 11:13 +0200
Message-ID<qotbny025hh.fsf@ruuvi.it.helsinki.fi>
In reply to#66813
Gary Herron writes:

> On 02/20/2014 10:49 PM, Jaydeep Patil wrote:
> > I am getting below tuple from excel.
> > How should i remove extra commas in each tuple to make it easy for
> > operations.
> >
> > tuples is:
> > seriesxlist1 = ((0.0), (0.01), (0.02), (0.03), (0.04), (0.05), (0.06), (0.07), (0.08), (0.09), (0.1), (0.11))
> >
> > please suggest me solution.
> 
> There are no extra *commas* there.  Perhaps you mean extra

There were extra commas in a previous thread.

Jaydeep, Rustom Mody gave you the answer, which you even quoted but
apparently failed to notice. Go back and see.

That answer was this:

   >>> seriesxlist1 = ((0.0,), (0.01,), (0.02,))
   >>> x2 = [x*x for (x,) in seriesxlist1]

I tend to omit those parentheses and use just the comma:

   >>> x2 = [x*x for x, in seriesxlist1]

[toc] | [prev] | [next] | [standalone]


#66821

FromAlister <alister.ware@ntlworld.com>
Date2014-02-21 09:29 +0000
Message-ID<E%ENu.15087$PQ7.1302@fx29.am4>
In reply to#66818
On Fri, 21 Feb 2014 11:13:30 +0200, Jussi Piitulainen wrote:

> Gary Herron writes:
> 
>> On 02/20/2014 10:49 PM, Jaydeep Patil wrote:
>> > I am getting below tuple from excel.
>> > How should i remove extra commas in each tuple to make it easy for
>> > operations.
>> >
>> > tuples is:
>> > seriesxlist1 = ((0.0), (0.01), (0.02), (0.03), (0.04), (0.05),
>> > (0.06), (0.07), (0.08), (0.09), (0.1), (0.11))
>> >
>> > please suggest me solution.
>> 
>> There are no extra *commas* there.  Perhaps you mean extra
> 
> There were extra commas in a previous thread.
> 
> Jaydeep, Rustom Mody gave you the answer, which you even quoted but
> apparently failed to notice. Go back and see.
> 
> That answer was this:
> 
>    >>> seriesxlist1 = ((0.0,), (0.01,), (0.02,))
>    >>> x2 = [x*x for (x,) in seriesxlist1]
> 
> I tend to omit those parentheses and use just the comma:
> 
>    >>> x2 = [x*x for x, in seriesxlist1]

I had not though of using unpacking in this way & would have written

x2= [x[0]**2 for x in serisexlist1]

I am not sure which is easier to read in this instance (single element 
tupple) but unpacking would definitely be the way to go if the tupple had 
multiple values.





-- 
Q:	What do they call the alphabet in Arkansas?
A:	The impossible dream.

[toc] | [prev] | [next] | [standalone]


#66831

FromTim Chase <python.list@tim.thechases.com>
Date2014-02-21 08:00 -0600
Message-ID<mailman.7228.1392991207.18130.python-list@python.org>
In reply to#66821
On 2014-02-21 09:29, Alister wrote:
> >    >>> seriesxlist1 = ((0.0,), (0.01,), (0.02,))
> >    >>> x2 = [x*x for (x,) in seriesxlist1]  
> > 
> > I tend to omit those parentheses and use just the comma:
> >   
> >    >>> x2 = [x*x for x, in seriesxlist1]  
> 
> I had not though of using unpacking in this way & would have written
> 
> x2= [x[0]**2 for x in serisexlist1]
> 
> I am not sure which is easier to read in this instance (single
> element tupple) but unpacking would definitely be the way to go if
> the tupple had multiple values.

With the single-value tuple, I tend to find the parens make it more
readable, so I'd go with

  [x*x for (x,) in lst]

whereas if they were multi-value tuples, I tend to omit the parens:

  [x*y for x,y in lst]

though, tangentially, Python throws a SyntaxError if you try and pass
a generator to a function without extra outer parens because it
makes parsing them ambiguous otherwise:

  >>>  x = sum(a+b for a, b in lst, 10)
  File "<stdin>", line 1
SyntaxError: Generator expression must be parenthesized if not sole
argument
  >>> x = sum((a+b) for a,b in lst), 10)
  [no error]

-tkc

[toc] | [prev] | [next] | [standalone]


#66834

FromPeter Otten <__peter__@web.de>
Date2014-02-21 15:14 +0100
Message-ID<mailman.7230.1392992078.18130.python-list@python.org>
In reply to#66821
Tim Chase wrote:

> With the single-value tuple, I tend to find the parens make it more
> readable, so I'd go with
> 
> [x*x for (x,) in lst]
 
Hardly ever seen in the wild, but unpacking works with [...], too:

>>> items = zip(range(5))
>>> [x*x for [x] in items]
[0, 1, 4, 9, 16]

[toc] | [prev] | [next] | [standalone]


#66835

FromRoy Smith <roy@panix.com>
Date2014-02-21 09:32 -0500
Message-ID<roy-7DBF08.09321721022014@news.panix.com>
In reply to#66834
In article <mailman.7230.1392992078.18130.python-list@python.org>,
 Peter Otten <__peter__@web.de> wrote:


> [x*x for (x,) in lst]
>
> [paraphrasing...] can be better written as:
>
> [x*x for [x] in items]

I'm torn between, "Yes, the second form is distinctly easier to read" 
and, "If you think the second form is easier to read, you're admitting 
you're not really fluent in Python".

[toc] | [prev] | [next] | [standalone]


#66836

FromTravis Griggs <travisgriggs@gmail.com>
Date2014-02-21 09:48 -0800
Message-ID<mailman.7231.1393004937.18130.python-list@python.org>
In reply to#66835
On Feb 21, 2014, at 6:32 AM, Roy Smith <roy@panix.com> wrote:

> In article <mailman.7230.1392992078.18130.python-list@python.org>,
> Peter Otten <__peter__@web.de> wrote:
> 
> 
>> [x*x for (x,) in lst]
>> 
>> [paraphrasing...] can be better written as:
>> 
>> [x*x for [x] in items]
> 
> I'm torn between, "Yes, the second form is distinctly easier to read" 
> and, "If you think the second form is easier to read, you're admitting 
> you're not really fluent in Python”.

I’ve used the comma form with struct.unpack() frequently:

count, = struct.unpack(‘!I’, self.packet)

That’s after I don’t use it and end up scratching my head for a while and finally remember that unpack returns a tuple regardless of how many things I unpack from it. It’s just natural if you’re doing lots of single unpacks to think it returns a single value. Either way, I much prefer it to:

count = struct.unpack(‘!I’, self.packet)[0]

[toc] | [prev] | [next] | [standalone]


#66838

FromTim Chase <python.list@tim.thechases.com>
Date2014-02-21 12:07 -0600
Message-ID<mailman.7233.1393005989.18130.python-list@python.org>
In reply to#66835
On 2014-02-21 09:48, Travis Griggs wrote:
> I’ve used the comma form with struct.unpack() frequently:
> 
> count, = struct.unpack(‘!I’, self.packet)

This is *especially* one of those places I want extra parens to make
sure I see what's happening. I've been stung too many times by the
easy-to-miss nature of just a single comma.

-tkc

[toc] | [prev] | [next] | [standalone]


#66956

FromCameron Simpson <cs@zip.com.au>
Date2014-02-24 12:19 +1100
Message-ID<mailman.7298.1393204758.18130.python-list@python.org>
In reply to#66835
On 21Feb2014 09:32, Roy Smith <roy@panix.com> wrote:
> In article <mailman.7230.1392992078.18130.python-list@python.org>,
>  Peter Otten <__peter__@web.de> wrote:
> > [x*x for (x,) in lst]
> >
> > [paraphrasing...] can be better written as:
> >
> > [x*x for [x] in items]
> 
> I'm torn between, "Yes, the second form is distinctly easier to read" 
> and, "If you think the second form is easier to read, you're admitting 
> you're not really fluent in Python".

+1 QOTW
-- 
Cameron Simpson <cs@zip.com.au>

It is a tale told by an idiot, full of sound and fury, signifying nothing.
        - William Shakespeare

[toc] | [prev] | [next] | [standalone]


#66832

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2014-02-21 14:02 +0000
Message-ID<mailman.7229.1392991347.18130.python-list@python.org>
In reply to#66795
On 21/02/2014 08:27, Gary Herron wrote:
>
> A bit of notation, because I''m not sure we are communicating well here:
>    A tuple is a Python data structure.  It has no commas or
> parentheses.   The *printing* of a Python tuple uses both for it's
> appearance on the output, but the tuple itself has no such thing.
>

 >>> a = 1,2,3
 >>> type(a)
<class 'tuple'>

I see commas and a tuple above but I don't see a print.

-- 
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.

Mark Lawrence

---
This email is free from viruses and malware because avast! Antivirus protection is active.
http://www.avast.com

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web