Python and arrays

戏子无情 提交于 2020-01-05 04:10:14

问题


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.

Help me, please :)


回答1:


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

b = map(float, a)



回答2:


 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



来源:https://stackoverflow.com/questions/6396733/python-and-arrays

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!