Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
您需要在浮子列表中转换您的列表,而不是字符串
a = [*map(float, a)] # [*map(float, a)] is equivalent to list(map(float, a)) a.sort() print(a)
可以工作。
如果要将其转换回str,则可以这样做:
a = [*map(str,a)]
或者,如果您不想落后零:
a = [*map(lambda c: str(round(c,2)).rstrip('0').rstrip('.'), a)]
如Shadowranger的指向,如果您希望它将其保存在Str中而无需在任何时候将其转换为float
a.sort(key = float)
,则
a = a.sorted(key = float)
You need to convert your list in a list of float, not of string
Should work.
If you want to convert it back to str you can do:
OR, if you don't want trailing zeros:
As pointed by ShadowRanger, if you want it to keep it in str without converting it in Float at any time you can do
Or
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
暂无简介
文章 0 评论 0
接受
发布评论
评论(1)
您需要在浮子列表中转换您的列表,而不是字符串
可以工作。
如果要将其转换回str,则可以这样做:
或者,如果您不想落后零:
如Shadowranger的指向,如果您希望它将其保存在Str中而无需在任何时候将其转换为float
,则
You need to convert your list in a list of float, not of string
Should work.
If you want to convert it back to str you can do:
OR, if you don't want trailing zeros:
As pointed by ShadowRanger, if you want it to keep it in str without converting it in Float at any time you can do
Or