Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #101016 > unrolled thread

Newbie: How to convert a tuple of strings into a tuple of ints

Started byotaksoftspamtrap@gmail.com
First post2015-12-30 14:46 -0800
Last post2015-12-30 16:00 -0700
Articles 5 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  Newbie: How to convert a tuple of strings into a tuple of ints otaksoftspamtrap@gmail.com - 2015-12-30 14:46 -0800
    Re: Newbie: How to convert a tuple of strings into a tuple of ints Chris Angelico <rosuav@gmail.com> - 2015-12-31 09:52 +1100
    Re: Newbie: How to convert a tuple of strings into a tuple of ints Ben Finney <ben+python@benfinney.id.au> - 2015-12-31 09:57 +1100
      Re: Newbie: How to convert a tuple of strings into a tuple of ints otaksoftspamtrap@gmail.com - 2015-12-30 15:00 -0800
    Re: Newbie: How to convert a tuple of strings into a tuple of ints Ian Kelly <ian.g.kelly@gmail.com> - 2015-12-30 16:00 -0700

#101016 — Newbie: How to convert a tuple of strings into a tuple of ints

Fromotaksoftspamtrap@gmail.com
Date2015-12-30 14:46 -0800
SubjectNewbie: How to convert a tuple of strings into a tuple of ints
Message-ID<fcd51ccc-b087-4147-a3e2-276b2f3052f4@googlegroups.com>
How do I get from here

t = ('1024', '1280')

to 

t = (1024, 1280)


Thanks for all help!

[toc] | [next] | [standalone]


#101017

FromChris Angelico <rosuav@gmail.com>
Date2015-12-31 09:52 +1100
Message-ID<mailman.89.1451515970.11925.python-list@python.org>
In reply to#101016
On Thu, Dec 31, 2015 at 9:46 AM,  <otaksoftspamtrap@gmail.com> wrote:
> How do I get from here
>
> t = ('1024', '1280')
>
> to
>
> t = (1024, 1280)
>
>
> Thanks for all help!

t = (int(t[0]), int(t[1]))

If the situation is more general than that, post your actual code and
we can help out more. Working with a single line isn't particularly
easy. :)

ChrisA

[toc] | [prev] | [next] | [standalone]


#101019

FromBen Finney <ben+python@benfinney.id.au>
Date2015-12-31 09:57 +1100
Message-ID<mailman.90.1451516257.11925.python-list@python.org>
In reply to#101016
otaksoftspamtrap@gmail.com writes:

> How do I get from here
>
> t = ('1024', '1280')
>
> to 
>
> t = (1024, 1280)

Both of those are assignment statements, so I'm not sure what you mean
by “get from … to”. To translate one assignment statement to a different
assignment statement, re-write the statement.


But I think you want to produce a new sequence from an existing sequence.

The ‘map’ built-in function is useful for that::

    sequence_of_numbers_as_text = ['1024', '1280']
    sequence_of_integers = map(int, sequence_of_numbers_as_text)

That sequence can then be iterated.

Another (more broadly useful) way is to use a generator expression::

    sequence_of_integers = (int(item) for item in sequence_of_numbers_as_text)


If you really want a tuple, just pass that sequence to the ‘tuple’
callable::

    tuple_of_integers = tuple(
            int(item) for item in sequence_of_numbers_as_text)

or::

    tuple_of_integers = tuple(map(int, sequence_of_numbers_as_text))

-- 
 \      “Nothing is more sacred than the facts.” —Sam Harris, _The End |
  `\                                                   of Faith_, 2004 |
_o__)                                                                  |
Ben Finney

[toc] | [prev] | [next] | [standalone]


#101020

Fromotaksoftspamtrap@gmail.com
Date2015-12-30 15:00 -0800
Message-ID<720808ec-ef8d-44b3-ae93-3a730b8d0922@googlegroups.com>
In reply to#101019
Thanks much - both solutions work well for me




On Wednesday, December 30, 2015 at 2:57:50 PM UTC-8, Ben Finney wrote:
> kierkegaard@gmail.com writes:
> 
> > How do I get from here
> >
> > t = ('1024', '1280')
> >
> > to 
> >
> > t = (1024, 1280)
> 
> Both of those are assignment statements, so I'm not sure what you mean
> by "get from ... to". To translate one assignment statement to a different
> assignment statement, re-write the statement.
> 
> 
> But I think you want to produce a new sequence from an existing sequence.
> 
> The 'map' built-in function is useful for that::
> 
>     sequence_of_numbers_as_text = ['1024', '1280']
>     sequence_of_integers = map(int, sequence_of_numbers_as_text)
> 
> That sequence can then be iterated.
> 
> Another (more broadly useful) way is to use a generator expression::
> 
>     sequence_of_integers = (int(item) for item in sequence_of_numbers_as_text)
> 
> 
> If you really want a tuple, just pass that sequence to the 'tuple'
> callable::
> 
>     tuple_of_integers = tuple(
>             int(item) for item in sequence_of_numbers_as_text)
> 
> or::
> 
>     tuple_of_integers = tuple(map(int, sequence_of_numbers_as_text))
> 
> -- 
>  \      "Nothing is more sacred than the facts." --Sam Harris, _The End |
>   `\                                                   of Faith_, 2004 |
> _o__)                                                                  |
> Ben Finney

[toc] | [prev] | [next] | [standalone]


#101021

FromIan Kelly <ian.g.kelly@gmail.com>
Date2015-12-30 16:00 -0700
Message-ID<mailman.91.1451516472.11925.python-list@python.org>
In reply to#101016
On Wed, Dec 30, 2015 at 3:46 PM,  <otaksoftspamtrap@gmail.com> wrote:
> How do I get from here
>
> t = ('1024', '1280')
>
> to
>
> t = (1024, 1280)

Deja vu: https://mail.python.org/pipermail/python-list/2015-December/701017.html

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web