开发者

Python and arrays

开发者 https://www.devze.com 2023-03-14 01:05 出处:网络
I know that python is a crazy language because of it\'s cycles constructions :) So, I have an array of numbers but in string type:

I know that python is a crazy language because of it's cycles constructions :)

So, I have an array of numbers but in string type:

a = ['1', '40', '356', '...']

I need this or a copy of this array but with float type instead of string. The only thing is that the code should be in one line.

Hel开发者_StackOverflowp me, please :)


You can use map()[docs] and float()[docs]:

b = map(float, a)


 a = ['1', '40', '356', '...']
 b = [float(x) for x in a]

This is called a list comprehension. It's a very powerful feature of Python, and you can read more about list comprehensions here:

http://docs.python.org/tutorial/datastructures.html#list-comprehensions

0

精彩评论

暂无评论...
验证码 换一张
取 消