Converting an array of string to array of float

2011-03-25 Thread joy99
Dear Group,

I got a question which might be possible but I am not getting how to
do it.

If I have a list, named,
list1=[1.0,2.3,4.4,5.5]

Now each element in the array holds the string property if I want to
convert them to float, how would I do it?

Extracting the values with for and appending to a blank list it would
not solve the problem. If appended to a blank list, it would not
change the property.

If any one of the learned members can kindly suggest any solution?

Thanks in advance.
Best Regards,
Subhabrata.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Converting an array of string to array of float

2011-03-25 Thread Nitin Pawar
did you try type casting with float ?

On Fri, Mar 25, 2011 at 8:49 PM, joy99  wrote:

> Dear Group,
>
> I got a question which might be possible but I am not getting how to
> do it.
>
> If I have a list, named,
> list1=[1.0,2.3,4.4,5.5]
>
> Now each element in the array holds the string property if I want to
> convert them to float, how would I do it?
>
> Extracting the values with for and appending to a blank list it would
> not solve the problem. If appended to a blank list, it would not
> change the property.
>
> If any one of the learned members can kindly suggest any solution?
>
> Thanks in advance.
> Best Regards,
> Subhabrata.
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
Nitin Pawar
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Converting an array of string to array of float

2011-03-25 Thread Jason Swails
I'm guessing you have something like

list1=['1.0', '2.3', '4.4', '5.5', ...], right?

You can do this:

for i in range(len(list1)):
  list1[i] = float(list1[i])

There's almost certainly a 1-liner you can use, but this should work.

--Jason

On Fri, Mar 25, 2011 at 8:19 AM, joy99  wrote:

> Dear Group,
>
> I got a question which might be possible but I am not getting how to
> do it.
>
> If I have a list, named,
> list1=[1.0,2.3,4.4,5.5]
>
> Now each element in the array holds the string property if I want to
> convert them to float, how would I do it?
>
> Extracting the values with for and appending to a blank list it would
> not solve the problem. If appended to a blank list, it would not
> change the property.
>
> If any one of the learned members can kindly suggest any solution?
>
> Thanks in advance.
> Best Regards,
> Subhabrata.
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Converting an array of string to array of float

2011-03-25 Thread Christoph Brand
If you want to have it even shorter and you are using Python 2.5 or 
greater you can also use:

list1 = [float(list_item) for list_item in list1]

Am 25.03.2011 16:27, schrieb Jason Swails:

I'm guessing you have something like

list1=['1.0', '2.3', '4.4', '5.5', ...], right?

You can do this:

for i in range(len(list1)):
  list1[i] = float(list1[i])

There's almost certainly a 1-liner you can use, but this should work.

--Jason

On Fri, Mar 25, 2011 at 8:19 AM, joy99 > wrote:


Dear Group,

I got a question which might be possible but I am not getting how to
do it.

If I have a list, named,
list1=[1.0,2.3,4.4,5.5]

Now each element in the array holds the string property if I want to
convert them to float, how would I do it?

Extracting the values with for and appending to a blank list it would
not solve the problem. If appended to a blank list, it would not
change the property.

If any one of the learned members can kindly suggest any solution?

Thanks in advance.
Best Regards,
Subhabrata.
--
http://mail.python.org/mailman/listinfo/python-list



-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Converting an array of string to array of float

2011-03-25 Thread Blockheads Oi Oi

On 25/03/2011 15:27, Jason Swails wrote:

I'm guessing you have something like

list1=['1.0', '2.3', '4.4', '5.5', ...], right?

You can do this:

for i in range(len(list1)):
   list1[i] = float(list1[i])



or

for i, x in enumerate(list1):
list1[i] = float(x)


--
http://mail.python.org/mailman/listinfo/python-list


Re: Converting an array of string to array of float

2011-03-25 Thread Rafael Durán Castañeda
But you must be sure that the list only contains objects than can be
converted into float, if not you'll get:

>>> float('something')
Traceback (most recent call last):
  File "", line 1, in 
float('something')
ValueError: could not convert string to float: something
>>>

2011/3/25 Blockheads Oi Oi 

> On 25/03/2011 15:27, Jason Swails wrote:
>
>> I'm guessing you have something like
>>
>> list1=['1.0', '2.3', '4.4', '5.5', ...], right?
>>
>> You can do this:
>>
>> for i in range(len(list1)):
>>   list1[i] = float(list1[i])
>>
>>
> or
>
> for i, x in enumerate(list1):
>list1[i] = float(x)
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Converting an array of string to array of float

2011-03-25 Thread Blockheads Oi Oi

On 25/03/2011 15:46, Rafael Durán Castañeda wrote:

But you must be sure that the list only contains objects than can be
converted into float, if not you'll get:

 >>> float('something')
Traceback (most recent call last):
   File "", line 1, in 
 float('something')
ValueError: could not convert string to float: something


You learn something new every day :)

--
http://mail.python.org/mailman/listinfo/python-list


Re: Converting an array of string to array of float

2011-03-25 Thread bruno.desthuilli...@gmail.com
On 25 mar, 16:19, joy99  wrote:
> Dear Group,
>
> I got a question which might be possible but I am not getting how to
> do it.
>
> If I have a list, named,
> list1=[1.0,2.3,4.4,5.5]
>
> Now each element in the array holds the string property if I want to
> convert them to float, how would I do it?
>
> Extracting the values with for and appending to a blank list it would
> not solve the problem. If appended to a blank list, it would not
> change the property.
>
> If any one of the learned members can kindly suggest any solution?
>


>>> print source
['0.0', '1.0', '2.0', '3.0', '4.0', '5.0', '6.0', '7.0', '8.0', '9.0']
>>> source[:] = map(float, source)
>>> print source
[0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]
>>>

Note the "source[:] = " part - it modifies the list in place.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Converting an array of string to array of float

2011-03-25 Thread joy99
On Mar 25, 9:19 pm, "bruno.desthuilli...@gmail.com"
 wrote:
> On 25 mar, 16:19, joy99  wrote:
>
>
>
> > Dear Group,
>
> > I got a question which might be possible but I am not getting how to
> > do it.
>
> > If I have a list, named,
> > list1=[1.0,2.3,4.4,5.5]
>
> > Now each element in the array holds the string property if I want to
> > convert them to float, how would I do it?
>
> > Extracting the values with for and appending to a blank list it would
> > not solve the problem. If appended to a blank list, it would not
> > change the property.
>
> > If any one of the learned members can kindly suggest any solution?
>
> >>> print source
>
> ['0.0', '1.0', '2.0', '3.0', '4.0', '5.0', '6.0', '7.0', '8.0', '9.0']>>> 
> source[:] = map(float, source)
> >>> print source
>
> [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]
>
>
>
> Note the "source[:] = " part - it modifies the list in place.

Thanks Bruno. I just missed it. I got it back and thanks Blockhead for
giving me new angle to look into the problem.
Best Regards,
Subhabrata.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Converting an array of string to array of float

2011-03-25 Thread Steven D'Aprano
On Fri, 25 Mar 2011 08:19:24 -0700, joy99 wrote:

> Dear Group,
> 
> I got a question which might be possible but I am not getting how to do
> it.
> 
> If I have a list, named,
> list1=[1.0,2.3,4.4,5.5]

That looks like a list of floats already. Perhaps you meant:

list1 = ["1.0", "2.3", "4.4", "5.5"]

Notice that the elements are now strings, not floats.


> Now each element in the array holds the string property if I want to
> convert them to float, how would I do it?
> 
> Extracting the values with for and appending to a blank list it would
> not solve the problem. If appended to a blank list, it would not change
> the property.

I don't understand what you mean. You want to get a list of floats. You 
create a list of floats. How does that not solve the problem?

Wait, do you mean you want to change them *in-place*? Like this:


>>> alist = ["1.1", "2.2", "3.3", "4.4", "5.5"]
>>> blist = alist  # reference to the same list object
>>> alist[:] = map(float, alist)
>>> print(blist[0] + 1)  # prove that it is an in-place change
2.1


The key is the slice assignment: map(float, alist) creates a new list, 
but the assignment alist[:] = ... stores those values in the original 
list, instead of just binding the name to a new object. It is equivalent 
to this:

temp = map(float, alist)
del alist[:]  # empty the list in place
alist.extend(temp)
del temp  # delete the variable

only easier and faster.


If you don't like map(), you can use a list comprehension:

alist[:] = [float(s) for s in alist]


Finally, if your list is so truly astonishingly huge that you don't have 
room for it and the list-of-floats at the same time, hundreds of millions 
of items or more, then you can change the list items in place:

for i, s in enumerate(alist):
alist[i] = float(s)


But for small lists and merely large lists (millions or tens of millions) 
of items, this will probably be much slower than the solutions shown 
above. But your MMV -- time your code and find out for yourself.


-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Converting an array of string to array of float

2011-03-25 Thread Algis Kabaila
On Saturday 26 March 2011 02:27:12 Jason Swails wrote:
> I'm guessing you have something like
> 
> list1=['1.0', '2.3', '4.4', '5.5', ...], right?
> 
> You can do this:
> 
> for i in range(len(list1)):
>   list1[i] = float(list1[i])

Better,

list1 = [float(v) for v in list1]

One statement only - long live list comprehension!

OldAl.
> 
> There's almost certainly a 1-liner you can use, but this
> should work.
> 
> --Jason
> 
> On Fri, Mar 25, 2011 at 8:19 AM, joy99 
 wrote:
> > Dear Group,
> > 
> > I got a question which might be possible but I am not
> > getting how to do it.
> > 
> > If I have a list, named,
> > list1=[1.0,2.3,4.4,5.5]
> > 
> > Now each element in the array holds the string property if
> > I want to convert them to float, how would I do it?
> > 
> > Extracting the values with for and appending to a blank
> > list it would not solve the problem. If appended to a
> > blank list, it would not change the property.
> > 
> > If any one of the learned members can kindly suggest any
> > solution?
> > 
> > Thanks in advance.
> > Best Regards,
> > Subhabrata.
> > --
> > http://mail.python.org/mailman/listinfo/python-list

-- 
Algis
http://akabaila.pcug.org.au/StructuralAnalysis.pdf
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Converting an array of string to array of float

2011-03-25 Thread Blockheads Oi Oi

Thanks Bruno. I just missed it. I got it back and thanks Blockhead for
giving me new angle to look into the problem.
Best Regards,
Subhabrata.


Nothing personal, but it's Blockheads (plural) AND you missed the OI OI. 
 What is the problem with modern day education? :)



--
http://mail.python.org/mailman/listinfo/python-list