【问题标题】:Get next and previous item in dictionary using Python使用 Python 获取字典中的下一个和上一个项目
【发布时间】:2019-10-07 05:54:40
【问题描述】:

我有一本字典如下:

defaultdict(None, {
    'Category 1': [
        {'id': 24, 'name': 'Video 1', 'cat_id': 16, 'tut_id': 14, 'description': ''}, 
        {'id': 28, 'name': 'Video 5', 'cat_id': 16, 'tut_id': 14, 'description': ''}
    ], 
    'Category 3': [
        {'id': 25, 'name': 'Video 3', 'cat_id': 18, 'tut_id': 14, 'description': ''}, 
        {'id': 29, 'name': 'Video 6', 'cat_id': 18, 'tut_id': 14, 'description': ''}
    ], 
    'Category 2': [
        {'id': 26, 'name': 'Video 2', 'cat_id': 17, 'tut_id': 14, 'description': ''}, 
        {'id': 27, 'name': 'Video 4', 'cat_id': 17, 'tut_id': 14, 'description': ''}
    ]
})

在我看来,我有下一个代码:

video_id = int(request.POST.get('id', None))
cat_id = int(request.POST.get('cat_id', None))
cat_name = int(request.POST.get('cat_name', None))
tut_id = int(request.POST.get('tut_id', None))

我想从字典中获取下一个上一个项。如果有相同类别的项目,则获取它们,如果没有,则从下一个或上一个类别中获取项目。

我不知道如何开始

预期输出

video_id = 3
cat_id = 18
cat_name = "Category 3"
tut_id = 14

next = {'id': 29, 'name': 'Video 6', 'cat_id': 18, 'tut_id': 14, 'description': ''}
prev = {'id': 28, 'name': 'Video 5', 'cat_id': 16, 'tut_id': 14, 'description': ''}

知道怎么做吗?

【问题讨论】:

标签: python


【解决方案1】:

defaultdict 可以像这样被展平为dictslist。假设您使用的是最新版本的 python,dict 对象会维护插入元素的顺序。

from pprint import pprint

d = defaultdict(None, {
    'Category 1': [
        {'id': 24, 'name': 'Video 1', 'cat_id': 16, 'tut_id': 14, 'description': ''}, 
        {'id': 28, 'name': 'Video 5', 'cat_id': 16, 'tut_id': 14, 'description': ''}
    ], 
    'Category 3': [
        {'id': 25, 'name': 'Video 3', 'cat_id': 18, 'tut_id': 14, 'description': ''}, 
        {'id': 29, 'name': 'Video 6', 'cat_id': 18, 'tut_id': 14, 'description': ''}
    ], 
    'Category 2': [
        {'id': 26, 'name': 'Video 2', 'cat_id': 17, 'tut_id': 14, 'description': ''}, 
        {'id': 27, 'name': 'Video 4', 'cat_id': 17, 'tut_id': 14, 'description': ''}
    ]
})

dicts = []
for v in d.values():
    dicts.extend(v)

pprint(dicts)

输出:

[{'cat_id': 16, 'description': '', 'id': 24, 'name': 'Video 1', 'tut_id': 14},
 {'cat_id': 16, 'description': '', 'id': 28, 'name': 'Video 5', 'tut_id': 14},
 {'cat_id': 18, 'description': '', 'id': 25, 'name': 'Video 3', 'tut_id': 14},
 {'cat_id': 18, 'description': '', 'id': 29, 'name': 'Video 6', 'tut_id': 14},
 {'cat_id': 17, 'description': '', 'id': 26, 'name': 'Video 2', 'tut_id': 14},
 {'cat_id': 17, 'description': '', 'id': 27, 'name': 'Video 4', 'tut_id': 14}]

据我推断,输出中不需要category name
以这种格式对数据进行整形后,可以使用 for 循环提取所需的数据。

video_id = 3
cat_id = 18
cat_name = "Category 3"
tut_id = 14


idx = -1
found = False
while(idx < len(dicts)):
    idx += 1
    video_id_i = int(dicts[idx]['name'].split()[1])
    if video_id == video_id_i:
        found = True
        break

# TODO: check for index out of bounds
if found:
    prev = dicts[idx - 1]
    curr = dicts[idx]
    next = dicts[idx + 1]

    print(prev, curr, next, sep='\n')

输出:

{'id': 28, 'name': 'Video 5', 'cat_id': 16, 'tut_id': 14, 'description': ''}
{'id': 25, 'name': 'Video 3', 'cat_id': 18, 'tut_id': 14, 'description': ''}
{'id': 29, 'name': 'Video 6', 'cat_id': 18, 'tut_id': 14, 'description': ''}

【讨论】:

    猜你喜欢
    • 2019-05-31
    • 2019-02-24
    • 1970-01-01
    • 2021-04-26
    • 1970-01-01
    • 2012-11-19
    • 1970-01-01
    • 2015-03-18
    • 2016-10-24
    相关资源
    最近更新 更多