Rumah  >  Soal Jawab  >  teks badan

python - 老铁们,在列表中如何对时间进行排序~

fx  = [{'次数': 2, '时间': '10:45:00'}, {'次数': 4, '时间': '14:58:00'}, {'次数': 5, '时间': '09:59:00'}, {'次数': 28, '时间': '08:13:00'}, {'次数': 34, '时间': '08:28:00'}, {'次数': 4, '时间': '05:12:00'}, {'次数': 12, '时间': '04:56:00'}]

上面的这个列表中,进行时间上的排列,比如最后那个是4点56的,排在第一位,9点59的那个排在最后面。

老铁们,帮帮忙啊~~谢谢

巴扎黑巴扎黑2741 hari yang lalu456

membalas semua(3)saya akan balas

  • 伊谢尔伦

    伊谢尔伦2017-04-18 10:36:39

    fx.sort(key=lambda item: int(item['时间'][:2])*60 + int(item['时间'][3:5]))

    balas
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-18 10:36:39

    Luangkan masa, desc

    balas
    0
  • 黄舟

    黄舟2017-04-18 10:36:39

        fx.sort(function (pre, next) {
            return new Date('1970/01/01 ' + pre['时间']) - new Date('1970/01/01 ' + next['时间'])
        });
        console.log(JSON.stringify(fx))

    balas
    0
  • Batalbalas