> For the complete documentation index, see [llms.txt](https://blog.cweihang.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://blog.cweihang.io/python/raw.md).

# 原生模块

Python 原生模块相关知识点

* 在Python3中，单斜杠/表示浮点除法，双斜杠//表示整数除法。
* StringIO在python2中可以用户保存bytes，但在python3中不能保存bytes，要用BytesIO

使用python实现大浮点数排序

```python
import functools

def sort_long_float(f1, f2):
    f1_words = f1.split('.')
    int_str1 = f1_words[0]
    float_str1 = f1_words[1].rstrip('0')
    f2_words = f2.split('.')
    int_str2 = f2_words[0]
    float_str2 = f2_words[1].rstrip('0')
    int1 = int(int_str1)
    int2 = int(int_str2)
    if int1 > int2:
        return 1
    elif int1 < int2:
        return -1
    else:
        if float_str1 > float_str2:
            return 1
        elif float_str1 < float_str2:
            return -1
        else:
            return 0

arr = ['0.5', '0.66', '0.50', '0.60', '0.10']

sorted_arr = sorted(arr, key=functools.cmp_to_key(sort_long_float))
print(sorted_arr)
```

主要利用了两个点：

* python中int可以表示无限大的整数
* 字符串表达小数部分，字符串比对是用字典序，跟小数部分的大小比较逻辑是一样的；
* functools可以用来实现类似C++ sort模板中复杂的cmp函数


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://blog.cweihang.io/python/raw.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
