473,385 Members | 1,518 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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. :-)
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 <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jinming Xu | last post by:
Sorry for the previous message. It's really a simple question and I have solved it myself. Thanks, Jinming ------------------------------------------------------------------------ Hi Folks,
66
by: Darren Dale | last post by:
Hello, def test(data): i = ? This is the line I have trouble with if i==1: return data else: return data a,b,c,d = test()
1
by: fedor | last post by:
Hi all, happy new year, I was trying to pickle a instance of a subclass of a tuple when I ran into a problem. Pickling doesn't work with HIGHEST_PROTOCOL. How should I rewrite my class so I can...
11
by: vdavidster | last post by:
Hello everyone, I want to convert a tuple to a list, and I expected this behavior: list(('abc','def')) -> list(('abc')) -> But Python gave me this behavior: list(('abc','def')) ->
3
by: Ken Durden | last post by:
Is it possible to force positive values to have the + sign prefixed on them? double f1 = 1024.2; double f2 = -1024.2; string.Format( "{0:F}", f1 ); // +1024.2 string.Format( "{0:F}", f2 );...
11
by: ago | last post by:
Inspired by some recent readings on LinuxJournal and an ASPN recipe, I decided to revamp my old python hack... The new code is a combination of (2) reduction methods and brute force and it is quite...
2
by: Alan Isaac | last post by:
I am probably confused about immutable types. But for now my questions boil down to these two: - what does ``tuple.__init__`` do? - what is the signature of ``tuple.__init__``? These...
1
by: Thirdworld | last post by:
I have a table called Contacts that hold the name of the contact, the phone number and the e-mail address. I'm trying to create an update query that can change the phone number or e-mail address for...
1
by: Alex Vinokur | last post by:
Hi, Is it possible to get number of elements in the tuple boost? For instance, boost::tuple<int, string, boolt; Number of elements in that tuple is 3. I need to get that number.
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.