我需要将浮动变成列表。 (Python 2.7.10) 例如
float = 7.434
list = [7, ".", 4, 3, 4]
我需要这个,所以我可以迭代浮动。
答案 0 :(得分:2)
如果 有列表:
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
(请注意,所有这些都是字符串...)
答案 1 :(得分:0)
[int(i) if i.isdigit() else i for i in map(str, str(number))]
示例:
>>> 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]
答案 2 :(得分:0)
只需list = list(str(float))
。
答案 3 :(得分:0)
我希望这是有用的: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