473,811 Members | 2,756 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1778
In article <ma************ *************** ***********@pyt hon.org>,
"Jinming Xu" <cy********@hot mail.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************* **********@yaho o.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.co m> declaimed
the following in comp.lang.pytho n:
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.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
=============== =============== =============== =============== == <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.ne tcom.com/> <

Jul 18 '05 #7
Roy Smith <ro*@panix.co m> wrote in message news:<ro******* *************** *@reader1.panix .com>...
br************* **********@yaho o.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.net com.com> wrote:
On Fri, 06 Aug 2004 18:15:13 -0400, Roy Smith <ro*@panix.co m> declaimed
the following in comp.lang.pytho n:
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

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

Similar topics

3
1377
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
5045
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
3265
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 pickle it? Thanks , Fedor
11
2939
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
28242
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 ); // -1024.2 I was hoping for something either in the string.Format prefix, or
11
4149
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 faster than the ASPN program. If anyone is interested I attached the code in http://agolb.blogspot.com/2006/01/sudoku-solver-in-python.html
2
3492
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 questions are stimulated by http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/303439 Looking at that, what fails if I leave out the following line? ::
1
1230
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 their respective contact by inputting the new information. I have tried creating one on my own but it only allows bulk updates. Can anyone help?
1
2322
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
9605
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10647
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10386
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10398
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9204
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7669
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6889
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5554
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3865
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.