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


Groups > comp.lang.python > #101020

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

Newsgroups comp.lang.python
Date 2015-12-30 15:00 -0800
References <fcd51ccc-b087-4147-a3e2-276b2f3052f4@googlegroups.com> <mailman.90.1451516257.11925.python-list@python.org>
Message-ID <720808ec-ef8d-44b3-ae93-3a730b8d0922@googlegroups.com> (permalink)
Subject Re: Newbie: How to convert a tuple of strings into a tuple of ints
From otaksoftspamtrap@gmail.com

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web