Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #101019
| From | Ben Finney <ben+python@benfinney.id.au> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Newbie: How to convert a tuple of strings into a tuple of ints |
| Date | 2015-12-31 09:57 +1100 |
| Message-ID | <mailman.90.1451516257.11925.python-list@python.org> (permalink) |
| References | <fcd51ccc-b087-4147-a3e2-276b2f3052f4@googlegroups.com> |
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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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