Skip to content
On this page

「学习一个」的归档方法


📍 2022-04-03 🏷️ #数据结构 #博客

问题

一个分类下的若干篇文档,已有规范的文件命名 %Y-%m-%d-title,归档须降序排列并根据年份分组。

解决方法

如 Category keyboard-photography 有如下内容:

python
posts = [
    {'category': '键盘摄影', 'date': '2016-09-09', 'slug': 'go-to-the-uk-next-week', 'tags': ['陈绮贞'], 'title': '下个星期去英国', 'year': '2016', 'file': '2016-09-09-go-to-the-uk-next-week.md'},
    {'category': '键盘摄影', 'date': '2021-07-23', 'slug': 'archery-on-horseback', 'tags': ['旅游', '采访'], 'title': '骑射', 'year': '2021', 'file': '2021-07-23-archery-on-horseback.md'},
    {'category': '键盘摄影', 'date': '2018-02-11', 'slug': 'nioh', 'tags': ['仁王', 'PS4'], 'title': '氢氧化镍完结撒花', 'year': '2018', 'file': '2018-02-11-nioh.md'},
    {'category': '键盘摄影', 'date': '2020-11-07', 'slug': 'buy-vegetables', 'tags': ['Leica', '', '扫街'], 'title': '买菜', 'year': '2020', 'file': '2020-11-07-buy-vegetables.md'},
    {'category': '键盘摄影', 'date': '2018-04-14', 'slug': 'an-uninhabited-island-without-friday', 'tags': ['', 'Cookie'], 'title': '没有星期五的无人岛', 'year': '2018', 'file': '2018-04-14-an-uninhabited-island-without-friday.md'}
]
posts = [
    {'category': '键盘摄影', 'date': '2016-09-09', 'slug': 'go-to-the-uk-next-week', 'tags': ['陈绮贞'], 'title': '下个星期去英国', 'year': '2016', 'file': '2016-09-09-go-to-the-uk-next-week.md'},
    {'category': '键盘摄影', 'date': '2021-07-23', 'slug': 'archery-on-horseback', 'tags': ['旅游', '采访'], 'title': '骑射', 'year': '2021', 'file': '2021-07-23-archery-on-horseback.md'},
    {'category': '键盘摄影', 'date': '2018-02-11', 'slug': 'nioh', 'tags': ['仁王', 'PS4'], 'title': '氢氧化镍完结撒花', 'year': '2018', 'file': '2018-02-11-nioh.md'},
    {'category': '键盘摄影', 'date': '2020-11-07', 'slug': 'buy-vegetables', 'tags': ['Leica', '人', '扫街'], 'title': '买菜', 'year': '2020', 'file': '2020-11-07-buy-vegetables.md'},
    {'category': '键盘摄影', 'date': '2018-04-14', 'slug': 'an-uninhabited-island-without-friday', 'tags': ['猫', 'Cookie'], 'title': '没有星期五的无人岛', 'year': '2018', 'file': '2018-04-14-an-uninhabited-island-without-friday.md'}
]

先按照时间(文件名)降序排列:

python
from operator import itemgetter
posts.sort(key=itemgetter('file'), reverse=True)
from operator import itemgetter
posts.sort(key=itemgetter('file'), reverse=True)

再根据年份分组:

python
from itertools import groupby
for year, group in groupby(posts, key=itemgetter('year')):
    for post in group:
        # Do something with current year and every post in this group.
        pass
from itertools import groupby
for year, group in groupby(posts, key=itemgetter('year')):
    for post in group:
        # Do something with current year and every post in this group.
        pass

上述方法分别用到了 operator.itemgetter()itertools.groupby() 函数。

实现

markdown
# 键盘摄影

> Archives["keyboard-photography"]

## 2022 年

- 03-13 - [按下了闹钟,开启另一个梦](./2022-03-13-press-the-alarm-clock-and-start-another-dream.md)
- 02-23 - [青岛啤酒](./2022-02-23-tsingtao-beer.md)
- 01-03 - [蛋挞](./2022-01-03-egg-tart.md)

## 2021 年

- 11-21 - [生命的谜底谁能知道藏在哪里](./2021-11-21-the-answers-to-lifes-riddles-who-knows-where-to-hide.md)
- 11-15 - [采访路上](./2021-11-15-on-the-way-to-interview.md)
- 08-05 - [栽出千万花的一生四季中径自盛放](./2021-08-05-planting-thousands-of-flowers-in-the-last-four-seasons.md)
- 07-23 - [骑射](./2021-07-23-archery-on-horseback.md)
- 05-18 - [比赛爬山](./2021-05-18-mountain-climbing.md)
- 04-11 - [无题](./2021-04-11-untitled-6.md)

......
# 键盘摄影

> Archives["keyboard-photography"]

## 2022 年

- 03-13 - [按下了闹钟,开启另一个梦](./2022-03-13-press-the-alarm-clock-and-start-another-dream.md)
- 02-23 - [青岛啤酒](./2022-02-23-tsingtao-beer.md)
- 01-03 - [蛋挞](./2022-01-03-egg-tart.md)

## 2021 年

- 11-21 - [生命的谜底谁能知道藏在哪里](./2021-11-21-the-answers-to-lifes-riddles-who-knows-where-to-hide.md)
- 11-15 - [采访路上](./2021-11-15-on-the-way-to-interview.md)
- 08-05 - [栽出千万花的一生四季中径自盛放](./2021-08-05-planting-thousands-of-flowers-in-the-last-four-seasons.md)
- 07-23 - [骑射](./2021-07-23-archery-on-horseback.md)
- 05-18 - [比赛爬山](./2021-05-18-mountain-climbing.md)
- 04-11 - [无题](./2021-04-11-untitled-6.md)

......

Powered by VitePress.