[Tutor] Converting all elements of a tuple
Remco Gerlich
scarblac@pino.selwerd.nl
Thu, 1 Jun 2000 15:42:23 +0200
On Thu, Jun 01, 2000 at 03:24:45PM +0200, André Dahlqvist wrote:
> If I have a tuple that consists of numbers that are represented as
> strings ('1.1', '1.2', '1.3') is there a simple way of converting all
> of these elements to their number representation, that is float?
>
> For now I use the for loop below for this, but since this must be a
> pretty common things to do I thought there might be a conversion
> function. Is there?
>
> str_tuple = ('1.1', '1.2', '1.3')
> lst = []
> for element in str_tuple:
> lst.append(float(element))
This is what map() was made for. Apply a function to all elements of a
sequence and return the list of results.
str_tuple = ('1.1','1.2','1.3')
lst = map(float, str_tuple)
--
Remco Gerlich, scarblac@pino.selwerd.nl