I got it! How to force a single number to be a tuple

Discussion in 'Python' started by Jinming Xu, Aug 4, 2004.

  1. Jinming Xu

    Jinming Xu Guest

    Sorry for the previous message. It's really a simple question and I have
    solved it myself.

    Thanks,

    Jinming

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

    _________________________________________________________________
    Discover the best of the best at MSN Luxury Living. http://lexus.msn.com/
     
    Jinming Xu, Aug 4, 2004
    #1
    1. Advertising

  2. Jinming Xu

    wes weston Guest

    Jinming Xu wrote:
    > Sorry for the previous message. It's really a simple question and I have
    > solved it myself.
    >
    > Thanks,
    >
    > Jinming
    >
    > ------------------------------------------------------------------------
    > 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
    >
    > _________________________________________________________________
    > Discover the best of the best at MSN Luxury Living. http://lexus.msn.com/
    >

    Jinming,

    >>> t = (1)
    >>> t

    1
    >>> t = (1,)
    >>> t

    (1,)
    >>>

    wes
     
    wes weston, Aug 4, 2004
    #2
    1. Advertising

  3. Jinming Xu wrote:
    > Sorry for the previous message. It's really a simple question and I have
    > solved it myself.
    >
    > Thanks,
    >
    > Jinming
    >
    > ------------------------------------------------------------------------
    > 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
    >
    > _________________________________________________________________
    > Discover the best of the best at MSN Luxury Living. http://lexus.msn.com/
    >

    Maybe something like:
    [Dbg]>>> y= 6
    [Dbg]>>> if not isinstance(x, (tp.ListType, tp.TupleType)):
    [Dbg]... y= (y, )
    [Dbg]...
    [Dbg]>>> print y
    (6,)
    [Dbg]>>>
    Colin W.
     
    Colin J. Williams, Aug 4, 2004
    #3
  4. Jinming Xu

    Jeff Sandys Guest

    y=2,
    The comma makes tuples.
    y=2, 3.0, 4.5,
    also works.

    Are you sure that you want a list of numbers as an immutable tuple?
    Tuples are intended for hetrogenious sequences like a data record,
    (like y=2, 3.0, 4.5 is a record of say quantity, price and weight,
    and y=3.0, 2, 4.5 is a completely different thing).
    Tuples can be used as an index in a dictionary.

    A list is mutable, intended for a homogenious sequence, the elements
    can be changed and a list can not be used a dictionary index.
    (like y=[2, 3.0, 4.5] is a list of the weights of three items and
    y=[3.0, 2, 4.5] has the same meaning)

    Thanks,
    Jeff Sandys

    Jinming Xu wrote:
    >
    > Sorry for the previous message. It's really a simple question and I have
    > solved it myself.
    >
    > Thanks,
    >
    > Jinming
    >
    > ------------------------------------------------------------------------
    > 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
     
    Jeff Sandys, Aug 5, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michal Mikolajczyk
    Replies:
    1
    Views:
    837
    Larry Bates
    Apr 20, 2004
  2. Jeff Epler
    Replies:
    0
    Views:
    1,045
    Jeff Epler
    Apr 20, 2004
  3. Bill Scherer
    Replies:
    0
    Views:
    636
    Bill Scherer
    Apr 20, 2004
  4. Jinming Xu
    Replies:
    10
    Views:
    811
    Bryan Olson
    Aug 8, 2004
  5. Gregor Horvath

    Why tuple with one item is no tuple

    Gregor Horvath, Mar 15, 2005, in forum: Python
    Replies:
    37
    Views:
    868
    Antoon Pardon
    Mar 30, 2005
Loading...

Share This Page