Python3:按元组第一个元素中包含的数据戳对元组列表进行排序

问题描述 投票:0回答:2

我在使用python排序时遇到困难。

[基本上,我有一个这样的列表,其中包含具有String和Dictionary的元组。该字符串是名称,并包含格式为'%Y-%m-%d_%Hh%Mm%Ss'的时间戳记]

示例:

[
    [
      "/lfs_store/history/snapshot_stack_instance_document_example_2020-05-12_05h14m58s.json",
      {
        "category": "history",
        "description": "an example of a document, using the stack_instance type",
        "name": "stack_instance_document_example_2020-05-12_05h14m58s",
        "type": "snapshot"
      }
    ],
    [
      "/lfs_store/history/snapshot_stack_instance_document_example_2020-05-11_11h53m27s.json",
      {
        "category": "history",
        "description": "an example of a document, using the stack_instance type",
        "name": "stack_instance_document_example_2020-05-11_11h53m27s",
        "type": "snapshot"
      }
    ],
    [
      "/lfs_store/history/snapshot_stack_instance_document_example_2020-05-11_11h49m26s.json",
      {
        "category": "history",
        "description": "an example of a document, using the stack_instance type",
        "name": "stack_instance_document_example_2020-05-11_11h49m26s",
        "type": "snapshot"
      }
    ],
    [
      "/lfs_store/history/snapshot_stack_instance_document_example_2020-05-11_11h50m27s.json",
      {
        "category": "history",
        "description": "an example of a document, using the stack_instance type",
        "name": "stack_instance_document_example_2020-05-11_11h50m27s",
        "type": "snapshot"
      }
    ],
    [
      "/lfs_store/history/snapshot_stack_instance_document_example_2020-05-11_11h50m54s.json",
      {
        "category": "history",
        "description": "an example of a document, using the stack_instance type",
        "name": "stack_instance_document_example_2020-05-11_11h50m54s",
        "type": "snapshot"
      }
    ],
    [
      "/lfs_store/history/snapshot_stack_instance_document_example2_2020-05-11_10h23m09s.json",
      {
        "category": "history",
        "description": "an example of a document, using the stack_instance type",
        "name": "stack_instance_document_example2_2020-05-11_10h23m09s",
        "type": "snapshot"
      }
    ],
    [
      "/lfs_store/history/snapshot_stack_instance_document_example2_2020-05-11_10h22m37s.json",
      {
        "category": "history",
        "description": "an example of a document, using the stack_instance type",
        "name": "stack_instance_document_example2_2020-05-11_10h22m37s",
        "type": "snapshot"
      }
    ]
]

我希望此列表根据时间戳进行排序,最好是最新列表为第一个元素。

我一直在尝试通过Python的sorted()方法执行此操作,但遇到了麻烦。它可能是正则表达式和lambda函数的组合,但我还没有使其正常工作。有人可以帮忙吗?

python sorting
2个回答
0
投票
def date_key(t): path, info = t date_regex = r"\d{4}-\d{2}-\d{2}_\d{2}h\d{2}m\d{2}s" m = re.search(date_regex, path) date = m.group(0) return date

然后:

xs.sort(key=date_key)

0
投票
data = [ ... your data ...] for item in data: print(item[0][-25:-5])

送礼

2020-05-12_05h14m58s
2020-05-11_11h53m27s
2020-05-11_11h49m26s
2020-05-11_11h50m27s
2020-05-11_11h50m54s
2020-05-11_10h23m09s
2020-05-11_10h22m37s

所以可以在切片时使用key=

data = sorted(data, reverse=True, key=lambda item:item[0][-25:-5])

完整示例
data = [ [ "/lfs_store/history/snapshot_stack_instance_document_example_2020-05-12_05h14m58s.json", { "category": "history", "description": "an example of a document, using the stack_instance type", "name": "stack_instance_document_example_2020-05-12_05h14m58s", "type": "snapshot" } ], [ "/lfs_store/history/snapshot_stack_instance_document_example_2020-05-11_11h53m27s.json", { "category": "history", "description": "an example of a document, using the stack_instance type", "name": "stack_instance_document_example_2020-05-11_11h53m27s", "type": "snapshot" } ], [ "/lfs_store/history/snapshot_stack_instance_document_example_2020-05-11_11h49m26s.json", { "category": "history", "description": "an example of a document, using the stack_instance type", "name": "stack_instance_document_example_2020-05-11_11h49m26s", "type": "snapshot" } ], [ "/lfs_store/history/snapshot_stack_instance_document_example_2020-05-11_11h50m27s.json", { "category": "history", "description": "an example of a document, using the stack_instance type", "name": "stack_instance_document_example_2020-05-11_11h50m27s", "type": "snapshot" } ], [ "/lfs_store/history/snapshot_stack_instance_document_example_2020-05-11_11h50m54s.json", { "category": "history", "description": "an example of a document, using the stack_instance type", "name": "stack_instance_document_example_2020-05-11_11h50m54s", "type": "snapshot" } ], [ "/lfs_store/history/snapshot_stack_instance_document_example2_2020-05-11_10h23m09s.json", { "category": "history", "description": "an example of a document, using the stack_instance type", "name": "stack_instance_document_example2_2020-05-11_10h23m09s", "type": "snapshot" } ], [ "/lfs_store/history/snapshot_stack_instance_document_example2_2020-05-11_10h22m37s.json", { "category": "history", "description": "an example of a document, using the stack_instance type", "name": "stack_instance_document_example2_2020-05-11_10h22m37s", "type": "snapshot" } ] ] #for item in data: # print(item[0][-25:-5]) data = sorted(data, reverse=True, key=lambda item:item[0][-25:-5]) print(data)
© www.soinside.com 2019 - 2024. All rights reserved.