470,826 Members | 1,811 Online

How to force a single number to be a tuple

Hi Folks,

I have a number sequence, which is put into a tuple like this:

y=2, 3.0, 4.5

I can manipulate the sequence as a tuple when it has more than 1 number. But
when the sequence has only 1 number, like

y=2

I have trouble to manipulate it as a tuple. I guess there must be a way to
forece a single number to be a tuple. Could anyone please tell me that?

Thanks,

Jinming

__________________________________________________ _______________
FREE pop-up blocking with the new MSN Toolbar – get it now!
http://toolbar.msn.click-url.com/go/...ave/direct/01/

Jul 18 '05 #1
10 1654
In article <ma**************************************@python.o rg>,
"Jinming Xu" <cy********@hotmail.com> wrote:
Hi Folks,

I have a number sequence, which is put into a tuple like this:

y=2, 3.0, 4.5

I can manipulate the sequence as a tuple when it has more than 1 number. But
when the sequence has only 1 number, like

y=2

I have trouble to manipulate it as a tuple. I guess there must be a way to
forece a single number to be a tuple. Could anyone please tell me that?

You're going to get a zillion responses to this one.

The syntax is a bit funky:

y = (2,)
Jul 18 '05 #2
You're going to get a zillion responses to this one.

The syntax is a bit funky:

y = (2,)

Funky. However, very logical. You can also write:

(1,2,3,4,)

(1,2,3,4)

The syntax is very clear and logical. (As usual when working with
python.) Try to add one comma for each element - that will do the stuff.
Most of the languages are not so straightforward - they forbid the last
comma.
Python is the best. :-)
Jul 18 '05 #3
Gandalf wrote:
y = (2,)
Funky. However, very logical. You can also write:

() == (,) File "<stdin>", line 1
() == (,)
^
SyntaxError: invalid syntax (1) == (1,) False (1, 2) == (1, 2,) True

Very logical indeed...
Python is the best. :-)

Of course, the above not withstanding :-)

Peter

Jul 18 '05 #4
Gandalf wrote:
The syntax is very clear and logical. (As usual when working with
python.) Try to add one comma for each element - that will do the stuff.
Most of the languages are not so straightforward - they forbid the last
comma.
The syntax may be logical, but it's not normal comma usage,.
Python is the best. :-)

Python tuples overlap too much with lists, and differ from the
functional/relational view in which a tuple is an element of the
Cartesian product of zero or more domains. Cartesian product is
associative; (1, (2, 3)) = ((1, 2), 3) and is canonically
written: (1, 2, 3). Any object is identical to the one-tuple of
that object. What Python calls 'tuples' are really immutable
lists.
--
--Bryan
Jul 18 '05 #5
br***********************@yahoo.com (Bryan Olson) wrote:
Python tuples overlap too much with lists, and differ from the
functional/relational view in which a tuple is an element of the
Cartesian product of zero or more domains.
For those of us who went to school a while ago, and perhaps didn't pay
as much attention in math class as we should have, could you translate
"an element of the Cartesian product of zero or more domains" into
English?
What Python calls 'tuples' are really immutable lists.

That's the way I've always thought of them. I know the cognoscenti will
insist that tuples are anonymous structures of heterogeneous types and
lists are ordered collections of homogenous data, but I don't buy the
distinction. There's nothing in the language that makes me think homo
vs. hetero for either. Maybe I'm being obstinate (my wife has certainly
accused me of that on one more than one occasion), or maybe I just swing
both ways when it comes to data containers, but that's the way I see it.

I remember once having a white-board discussion with some C++ friends of
mine where we were talking about writing code to parse things like:

insert into foo values (1, 2, "three", "four");

My Python code built up a list of the values and generated [1, 2,
"three", "four"]. My two friends recoiled violently at the idea that I
would put heterogeneous data types into a list. I passed it off as
simply being due to their poor unfortunate upbringing in the C++/STL
world of type bondage, while I was living in the carefree bohemian
Python world. I was shocked to discover some time later that Python was
not as bohemian as I thought, and the priests and elders would have been
as dismayed at my carefree mixing of data types in a list as my stodgy
C++ brethren were.

I personally think tuples should have used <> instead of (). It would
have resolved a lot of notational ambiguity.
Jul 18 '05 #6
On Fri, 06 Aug 2004 18:15:13 -0400, Roy Smith <ro*@panix.com> declaimed
the following in comp.lang.python:
For those of us who went to school a while ago, and perhaps didn't pay
as much attention in math class as we should have, could you translate
"an element of the Cartesian product of zero or more domains" into
English?
It's more the terminology of relation database theory. Might
have derived from some esoteric set theory in match, but for the most
part relational database theory terms map to "common" terms as:

relation table
tuple row (record)
domain column

An unrestricted Cartesian product basically pairs up each entry
of "domain A" with each entry of "domain B"... Take the stereotypical
Chinese restaurant menu: one from column A, one from column B, one from
column C... Now write out ALL possible combinations. That is the
Cartesian product, and each row is a tuple.

"domain A" "domain B"
me one
myself two
I

product
me one
me two
myself one
myself two
I one
I two

(a) tuple
me two
-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Jul 18 '05 #7
Roy Smith <ro*@panix.com> wrote in message news:<ro***********************@reader1.panix.com> ...
br***********************@yahoo.com (Bryan Olson) wrote:
Python tuples overlap too much with lists, and differ from the
functional/relational view in which a tuple is an element of the
Cartesian product of zero or more domains.

For those of us who went to school a while ago, and perhaps didn't pay
as much attention in math class as we should have, could you translate
"an element of the Cartesian product of zero or more domains" into
English?

The Cartesian product of X and Y is the set [(x, y) for x in X for y in Y].
Jul 18 '05 #8
In article <ui********************************@4ax.com>,
Dennis Lee Bieber <wl*****@ix.netcom.com> wrote:
On Fri, 06 Aug 2004 18:15:13 -0400, Roy Smith <ro*@panix.com> declaimed
the following in comp.lang.python:
For those of us who went to school a while ago, and perhaps didn't pay
as much attention in math class as we should have, could you translate
"an element of the Cartesian product of zero or more domains" into
English?

It's more the terminology of relation database theory. Might
have derived from some esoteric set theory in match, but for the most
part relational database theory terms map to "common" terms as:

relation table
tuple row (record)
domain column

An unrestricted Cartesian product basically pairs up each entry
of "domain A" with each entry of "domain B"

Ah. The cross-product. It's amazing how so many fields of study use
the same concepts but invent new names for things to obfuscate
everything :-)
Jul 18 '05 #9
Roy Smith wrote:
Dennis Lee Bieber
An unrestricted Cartesian product basically pairs up each entry
of "domain A" with each entry of "domain B"

Ah. The cross-product. It's amazing how so many fields of study use
the same concepts but invent new names for things to obfuscate
everything :-)

The Cartesian product is named for René Descartes, father of
analytic geometry. Though sometimes called the 'Cross product',
the latter term is much more commonly associated with the vector
cross-product, and that's what people will usually find if they
try to look up or Google "cross-product".
--
--Bryan
Jul 18 '05 #10
Roy Smith wrote:
For those of us who went to school a while ago, and perhaps didn't pay
as much attention in math class as we should have, could you translate
"an element of the Cartesian product of zero or more domains" into
English?
A domain is a set; it's used in the contexts like the domain of
a function, where it means the values for which the function is
defined. I see others have explained the Cartesian product.

[...] My Python code built up a list of the values and generated [1, 2,
"three", "four"]. My two friends recoiled violently at the idea that I
would put heterogeneous data types into a list. I passed it off as
simply being due to their poor unfortunate upbringing in the C++/STL
world of type bondage, while I was living in the carefree bohemian
Python world. I was shocked to discover some time later that Python was
not as bohemian as I thought, and the priests and elders would have been
as dismayed at my carefree mixing of data types in a list as my stodgy
C++ brethren were.

Perhaps those priests and elders would be happier in ML or one
of its followers. There's a lot to be said for uniform-type
lists, but that's not Python's idea of lists.
--
--Bryan
Jul 18 '05 #11