How to force a single number to be a tuple

J

Jinming Xu

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/onm00200415ave/direct/01/
 
R

Roy Smith

"Jinming Xu said:
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,)
 
G

Gandalf

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,)

instead of

(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. :)
 
B

Bryan Olson

Gandalf said:
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.
 
R

Roy Smith

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

Dennis Lee Bieber

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


--
 
D

Dan Bishop

Roy Smith said:
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].
 
R

Roy Smith

Dennis Lee Bieber said:
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 :)
 
B

Bryan Olson

Roy said:
> Dennis Lee Bieber
>
> 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".
 
B

Bryan Olson

Roy said:
> 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.
 

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

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top