V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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
maloneleo88
V2EX  ›  Python

Q:怎么给一个乱序元组列表按时间排序?

  •  
  •   maloneleo88 · 2021-04-01 21:50:54 +08:00 · 1504 次点击
    这是一个创建于 1114 天前的主题,其中的信息可能已经有所发展或是发生改变。
    list = [('2021-03-18 21:30', '16863', '4032', '0', '3'),('2021-03-18 19:15', '42898', '9917', '2', '2'),('2021-03-16 21:30', '9918', '7239', '1', '0'),('2021-03-16 19:15', '9916', '16988', '1', '0'),('2021-03-09 21:30', '9924', '9914', '0', '1'),('2021-03-18 21:30', '16863', '4032', '0', '3')]

    要转换日期时间成时间戳吗?还是有更简单的方法?主要列表里是元组,应该怎么写呢?
    我想倒序,是按正序排完了再 reversed 吗?
    13 条回复    2021-04-01 22:49:48 +08:00
    touchwithe
        1
    touchwithe  
       2021-04-01 22:06:12 +08:00 via iPhone
    list.sort(lambda item: datetime.datetime.strptime(item[0], ‘%Y-%m-%D %H:%M’), reverse=True)
    手机回复,没测试,应该是这样的。看一下 list.sort 的两个参数就知道了。
    echowuhao
        2
    echowuhao  
       2021-04-01 22:10:24 +08:00 via Android
    不转换直接用 str 比较应该也是对的吧
    infun
        3
    infun  
       2021-04-01 22:13:27 +08:00
    ```python
    import time

    def take_first(elem):
    time_string = elem[0]
    return time_string

    input_list = [('2021-03-18 21:30', '16863', '4032', '0', '3'),('2021-03-18 19:15', '42898', '9917', '2', '2'),('2021-03-16 21:30', '9918', '7239', '1', '0'),('2021-03-16 19:15', '9916', '16988', '1', '0'),('2021-03-09 21:30', '9924', '9914', '0', '1'),('2021-03-18 21:30', '16863', '4032', '0', '3')]

    input_list.sort(key=take_first, reverse=True)

    print(input_list)
    ```
    Gorgine
        4
    Gorgine  
       2021-04-01 22:14:25 +08:00
    list = [('2021-03-18 21:30', '16863', '4032', '0', '3'),('2021-03-18 19:15', '42898', '9917', '2', '2'),('2021-03-16 21:30', '9918', '7239', '1', '0'),('2021-03-16 19:15', '9916', '16988', '1', '0'),('2021-03-09 21:30', '9924', '9914', '0', '1'),('2021-03-18 21:30', '16863', '4032', '0', '3')]
    sorted_list = sorted(list, key=lambda x: x[0], reverse=True)
    print(sorted_list)
    infun
        5
    infun  
       2021-04-01 22:15:42 +08:00
    楼上这个更好
    maloneleo88
        6
    maloneleo88  
    OP
       2021-04-01 22:25:01 +08:00
    @touchwithe
    @infun
    @echowuhao
    @infun
    谢谢啊 !!!

    @Gorgine 这么骚的操作是怎么做到的, 也没用 datatme 。 讲讲啊大神 lambda 不太懂,不就是匿名函数吗? 怎么就按时间排列了呢??
    touchwithe
        7
    touchwithe  
       2021-04-01 22:31:52 +08:00 via iPhone
    @maloneleo88 字符串排序是逐个比较字符,所以正好和日期排序的结果一样
    Gorgine
        8
    Gorgine  
       2021-04-01 22:33:16 +08:00
    对 时间比较没必要转成 datetime 或者时间戳 字符串也是可以比较的

    sorted 有个参数叫 key, 这个 key 可以传个函数进去,函数的参数默认就是这个 list 的 item,就是每个元祖,所以在这个匿名函数里面取 x[0],就取到来元祖的第一项,也就是那个时间字符串,然后 sorted 会根据 x[0]来给这个 list 排序
    maloneleo88
        9
    maloneleo88  
    OP
       2021-04-01 22:36:11 +08:00
    @touchwithe
    @Gorgine

    666

    擦,不学了。 我这撅腚捅咕一个小时没捅咕出来。。

    老大们 接活吗?
    Gorgine
        10
    Gorgine  
       2021-04-01 22:37:59 +08:00
    @maloneleo88 接咋了
    maloneleo88
        11
    maloneleo88  
    OP
       2021-04-01 22:41:33 +08:00 via iPhone
    @Gorgine 好的 等有机会的
    lululau
        12
    lululau  
       2021-04-01 22:47:03 +08:00
    sorted_list = sorted(list, key=operator.itemgetter(0), reverse=True)
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2587 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 04:23 · PVG 12:23 · LAX 21:23 · JFK 00:23
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.