推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
kknd22
V2EX  ›  Python

Python 排序如何做到与 Windows 一致?

  •  
  •   kknd22 · Oct 12, 2015 · 3967 views
    This topic created in 4055 days ago, the information mentioned may be changed or developed.
    由于特殊需求,要让 Python 排序完全符合 windows 按名称排序的规则排序文件名,这样应该如何做到?
    比如几个文件名排序后, python 会把 "win_1.txt" 放到"_win1.txt"前边,而 windows 则会相反,特殊符号在前。
    9 replies  •  2015-10-14 13:20:04 +08:00
    codeforlife
    codeforlife
    Oct 12, 2015
    1
    列表排序么
    kknd22
    kknd22
    OP
    Oct 12, 2015
    2
    @codeforlife 是的,列表排序
    codeforlife
    codeforlife
    Oct 12, 2015
    3
    @kknd22
    你可以通过比较函数,先定义比较函数:

    def compare(x, y):
    x - y

    然后排序 :
    list.sort(cmp = compare)
    tobyxdd
    tobyxdd
    Oct 12, 2015 via Android
    4
    windows 排序大体规则还是按字母的。。特殊的既然规则也已经知道了就自己微调?
    staticor
    staticor
    Oct 12, 2015
    5
    map 字符串每个字符 -> ascii 值 (或自定义); 再据此排序
    Strikeactor
    Strikeactor
    Oct 12, 2015
    6
    自己写呗。。你还希望 Pyhton 专门做个函数去兼容 Windows 的排序么
    DonaidTrump
    DonaidTrump
    Oct 12, 2015
    7
    <img src="//cdn.v2ex.co/avatar/8091/58b6/131707_normal.png?m=1439002137" class="avatar" border="0" align="default">
    guoyijun163
    guoyijun163
    Oct 13, 2015❤️2
    8
    Shlwapi = windll.LoadLibrary("Shlwapi")
    @WINFUNCTYPE(c_int, LPCWSTR, LPCWSTR)
    def compare_string(first_str, second_str):
    return Shlwapi.StrCmpLogicalW(first_str, second_str)

    调用一个 WIN32 API
    kknd22
    kknd22
    OP
    Oct 14, 2015
    9
    @guoyijun163
    用这个方法搞定了
    另外谢谢所有朋友
    @codeforlife @tobyxdd @staticor @Strikeactor @tulongtou @guoyijun163
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   4350 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 09:08 · PVG 17:08 · LAX 01:08 · JFK 04:08
    ♥ Do have faith in what you're doing.