![](/img/trans.png)
[英]Sorting list of tuples by first element of each tuple - Python 2.7
[英]Python3: Sorting a list of tuples by datastamp contained in first element of tuple
我在 python 中使用排序有困難。
基本上,我有一個這樣的列表,其中包含具有字符串和字典的元組。 該字符串是名稱並包含格式為“%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 function 的組合,但我還沒有設法讓它工作。 有人可以幫忙嗎?
您在字符串中的同一位置有日期和時間,因此您可以使用切片來獲取它並進行比較,而無需轉換為時間戳。
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)
這是您可以使用的key
:
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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.