按组成部分对字符串列表进行排序 [英] Sorting a list of strings by their components

查看:75
本文介绍了按组成部分对字符串列表进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一长串包含一些要排序的元素.

A long list contains some elements to be sorted.

实际上,每个元素都有4个内容:名称,输入/输出,区域和日期和时间,并以〜"开头. (可以更改〜".)我想将列表重新组织为已排序的顺序.

Actually each element has 4 contents: name, in/out, area and date&time, joined by ‘~’. (The ‘~’ can be changed.) I want to reorganize the list into a sorted order.

a_list = ["Chris~Check-in~Zoom A~11/13/2013 05:20",
"Chris~Check-in~Zoom G~11/15/2013 14:09",
"Frank E~Check-in~Zoom K~11/11/2013 08:48",
"Frank E~Check-in~Zoom K~11/15/2013 21:32",
"Kala Lu S~Check-in~Zoom N~11/13/2013 07:20",
"Milly Emily~Check-in~Zoom G~11/13/2013 01:08",
"Milly Emily~Check-in~Zoom E~11/16/2013 14:39",
"Milly Amy~Check-in~Zoom G~11/10/2013 20:14",
"Milly Amy~Check-in~Zoom A~11/16/2013 08:55",
"Milly Amy~Check-in~Zoom O~11/14/2013 21:57",
"Milly Amy~Check-in~Zoom A~11/15/2013 10:45",
"Nago Iko~Check-in~Zoom K~11/16/2013 20:42",
"Nago Iko~Check-in~Zoom K~11/14/2013 10:46",
"Liz D~Check-in~Zoom N~11/15/2013 01:46",
"Liz D~Check-in~Zoom A~11/12/2013 09:54",
"Liz D~Check-in~Zoom G~11/16/2013 13:15",
"Chris~Check-out~Zoom A~11/13/2013 13:42",
"Chris~Check-out~Zoom G~11/11/2013 14:21",
"Chris~Check-out~Zoom G~11/16/2013 09:41",
"Frank E~Check-out~Zoom K~11/14/2013 03:02",
"House P~Check-out~Zoom K~11/10/2013 11:17",
"Kala Lu S~Check-out~Zoom G~11/11/2013 23:27",
"Kala Lu S~Check-out~Zoom N~11/14/2013 11:17"]

它可以导入到MS Excel中并进行排序,但是我想知道Python是否可以完成这项工作.

It can be imported into MS Excel and sorted, but I am wondering if Python can do the job.

是否可以按列表中的顺序对它们进行排序:1.名称,2.日期和时间3.区域4.进/出?例如:

Would it be possible to sort them by the order in the list: 1. name, 2. date&time 3. area 4. in/out ?Like:

new_list = ["Chris~Check-out~Zoom G~11/08/2014 14:21",
"Chris~Check-in~Zoom A~11/10/2014 05:20",
"Chris~Check-out~Zoom A~11/10/2014 13:42",
"Chris~Check-in~Zoom G~11/12/2014 14:09",
"Chris~Check-out~Zoom G~11/13/2014 09:41",
"Frank E~Check-in~Zoom K~11/08/2014 08:48",
"Frank E~Check-out~Zoom K~11/11/2014 03:02",
"Frank E~Check-in~Zoom K~11/12/2014 21:32",
...
...]

谢谢.

推荐答案

您可以拆分列表,然后使用自定义的key函数进行排序.但是您需要先解析日期才能正确对它们进行排序.

You can split the list, then sort with a custom key function. But you need to parse the date first to sort them correctly.

import datetime

new_l = sorted((x.split('~') for x in l),
               key=lambda x: (x[0],
                              datetime.datetime.strptime(x[3], '%m/%d/%Y %H:%M'),
                              x[2],
                              x[1]))

键函数返回一个元组.元组在字典上进行比较;比较第一项;如果它们相同,则比较第二个项目,依此类推.等等. [1]

The key function returns a tuple. Tuples are compared lexicographically; the first items are compared; if they are the same then the second items are compared, and so on.[1]

或者,您可以分阶段进行排序.这将允许您指定要按升序或降序排序的列.

Alternatively, you can sort in stages. This will allow you to specify columns to sort by ascending or descending order individually.

from operator import itemgetter

nl = [x.split('~') for x in l]

nl.sort(key=itemgetter(1))
nl.sort(key=itemgetter(2))
nl.sort(key=lambda x: datetime.datetime.strptime(x[3], '%m/%d/%Y %H:%M'),
        reverse=True) # Newest first
nl.sort(key=itemgetter(0))

请记住,两种方式都会使新列表按如下方式分割:

Keep in mind that both ways will make the new list split like this:

new_list = [["Chris", "Check-out", "Zoom G", "11/08/2014 14:21"], ...]

如果要将它们更改回原始形式,可以join它们:

If you want to change them back to the original form you can join them:

new_list_joined = ['~'.join(x) for x in new_list]

这篇关于按组成部分对字符串列表进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆