如何将集合转换为浮点类型的列表?

How to convert set into list of float type?

我有一个 mark_list 浮点类型,我试图在其中按升序排序。

mark_list
Out[100]: [39, 37.21, 38, 37.21, 37.2]

sorted(mark_list)
Out[101]: [37.2, 37.21, 37.21, 38, 39]

set(sorted(mark_list))
Out[102]: {37.2, 37.21, 38, 39}

list(set(sorted(mark_list)))
Out[103]: [37.21, 37.2, 38, 39]

我期望输出 [37.2, 37.21, 38, 39]

为什么 37.21 比 37.2 早?我错过了什么?

集未排序。如果你希望有一个排序列表,首先获取集合,然后转换为列表,最后排序。

实际上,您不想在这种情况下使用 set。 重要的是要考虑:

一个set

is an unordered collection with no duplicate elements. Basic uses include membership testing and eliminating duplicate entries.

您可以在 Python docs

阅读更多内容