Remove comma from tuples in python.


J

Jaydeep Patil

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
 
Ad

Advertisements

M

Mircescu Andrei

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
 
S

Stephane Wirtel

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.
 
B

Bernd Nawothnig

vineri, 21 februarie 2014, 08:49:01 UTC+2, Jaydeep Patil a scris:

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

No it isn't:

#v+(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+#v-




Bernd
 
G

Gary Herron

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.
(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
 
J

Jussi Piitulainen

Gary said:
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]
 
Ad

Advertisements

T

Tim Chase

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:
File "<stdin>", line 1
SyntaxError: Generator expression must be parenthesized if not sole
argument[no error]

-tkc
 
M

Mark Lawrence

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.
<class 'tuple'>

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

Roy Smith

[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".
 
T

Travis Griggs

[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]
 
Ad

Advertisements

T

Tim Chase

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
 
Ad

Advertisements

C

Cameron Simpson

[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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top