python中的分类功能不适合长列表?

发布于 2025-02-07 03:35:52 字数 1399 浏览 1 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

剑心龙吟 2025-02-14 03:35:52

您需要在浮子列表中转换您的列表,而不是字符串

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

a = [*map(float, a)] #  [*map(float, a)] is equivalent to list(map(float, a))
a.sort()
print(a)

Should work.

If you want to convert it back to str you can do:

a = [*map(str,a)]

OR, if you don't want trailing zeros:

a = [*map(lambda c: str(round(c,2)).rstrip('0').rstrip('.'), a)]

As pointed by ShadowRanger, if you want it to keep it in str without converting it in Float at any time you can do

a.sort(key = float)

Or

a = a.sorted(key = float)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文