如何将浮点转换为列表。(Python)
我需要把一个浮点数变成一个列表。(Python 2.7.10) 例如 我需要这个,这样我就可以迭代浮点。如果您必须有一个列表:如何将浮点转换为列表。(Python),python,list,python-2.7,Python,List,Python 2.7,我需要把一个浮点数变成一个列表。(Python 2.7.10) 例如 我需要这个,这样我就可以迭代浮点。如果您必须有一个列表: list(int(i) if i.isdigit() else i for i in str(7.345)) 但是你可以迭代一个字符串 for i in str(-7.345): print i - 7 . 3 4 5 (请注意,所有这些都是字符串…) 例如: >>> num = 1.2345 >>> [int(i) if i.i
list(int(i) if i.isdigit() else i for i in str(7.345))
但是你可以迭代一个字符串
for i in str(-7.345): print i
-
7
.
3
4
5
(请注意,所有这些都是字符串…)
例如:
>>> num = 1.2345
>>> [int(i) if i.isdigit() else i for i in map(str, str(num))]
[1, '.', 2, 3, 4, 5]
>>> num = -1.2345
>>> [int(i) if i.isdigit() else i for i in map(str, str(num))]
['-', 1, '.', 2, 3, 4, 5]
简单地
list=list(str(float))
我希望这是有用的:D
#You have a float like this :` float = 7.434`
#Now , let's convert the "float" variable to a list:
float=7.434
LIST=[]
float_to_str=str(float)
for elem in float_to_str:
#In elem is a dot ( . ) :
if elem==".":
LIST.append(elem)
else:
#If the element is a number:
LIST.append( int(elem) )
# In case you want to see you list :
print(str(LIST))
#GOOD LUCK :D
#WATERFULL IDR is planning to earn money with python , if you want to join me making and
#selling games , don't hesitate : facebook : Waterfull Idr ------- GOOD LUCK ----------
#;D
list(int(x)if x.isdigit()else x for x in str(float))
如果您不关心精度,“迭代浮点”是什么意思?为什么你认为你需要这样做?这个答案确实没有错,但我很好奇为什么一开始你会想这样做……有很多欧拉计划的问题,你必须根据,比如,数字和数字的总和来做一些事情。诚然,至少有70%的时候,你应该强烈考虑使用计数器
,并重新思考你的解决方案,但这是一件事。
>>> num = 1.2345
>>> [int(i) if i.isdigit() else i for i in map(str, str(num))]
[1, '.', 2, 3, 4, 5]
>>> num = -1.2345
>>> [int(i) if i.isdigit() else i for i in map(str, str(num))]
['-', 1, '.', 2, 3, 4, 5]
#You have a float like this :` float = 7.434`
#Now , let's convert the "float" variable to a list:
float=7.434
LIST=[]
float_to_str=str(float)
for elem in float_to_str:
#In elem is a dot ( . ) :
if elem==".":
LIST.append(elem)
else:
#If the element is a number:
LIST.append( int(elem) )
# In case you want to see you list :
print(str(LIST))
#GOOD LUCK :D
#WATERFULL IDR is planning to earn money with python , if you want to join me making and
#selling games , don't hesitate : facebook : Waterfull Idr ------- GOOD LUCK ----------
#;D