首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在python中构建5级树结构

在python中构建5级树结构
EN

Stack Overflow用户
提问于 2012-03-27 20:37:30
回答 2查看 820关注 0票数 1

我想减少命中数据库以检索数据的次数。因此,我认为将整个数据放入树中会提高系统的性能,因为我不会频繁访问数据库。

我是python的初学者,所以请在创建树结构时给我一些帮助和建议。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-03-27 21:03:41

您可以使用嵌套dictionaries。嵌套意味着键:值对的值可以是另一个字典。

JerseyMike给出了一个很好的例子,我只想指出他的addItemAttributes函数相当于更简洁

代码语言:javascript
复制
def addItemAttributes(tree, idList):
    (menu, cat, subcat, item, attribs) = idList;

    currDict = tree.setdefault(menu, {})\
        .setdefault(cat, {})\
        .setdefault(subcat, {})\
        .setdefault(item, {})

    for a in attribs:
        currDict[a[0]] = a[1]

您可能希望将getItemAttributes包装在...and块中,这样您就可以处理缺少其中一个键的情况,例如。

代码语言:javascript
复制
try:
    getItemAttributes(...)
except KeyError:
    #key was incorrect, deal with the situation
票数 1
EN

Stack Overflow用户

发布于 2012-03-28 00:01:52

因为我手头没有Python解释器,所以我就把这些放在一起了。下面是两个用于填充和读取嵌套字典结构的函数。传递给每个对象的第一个参数是保存所有菜单信息的基本字典。

此函数用于添加到字典中。这个函数在代码方面可能会更高效,但我希望您能理解所发生的事情。对于每个菜单类别的每个子类别的每个项目,您将需要构造一个要传递的属性列表。

代码语言:javascript
复制
# idList is a tuple consisting of the following elements:
#    menu: string   - menu name
#    cat: string    - category name
#    subcat: string - subcategory name
#    item: string   - item name
#    attribs: list  - a list of the attributes tied to this item in the form of
#                     [('Price', '7.95'),('ContainsPeanuts', 'Yes'),('Vegan', 'No'),...].
#                     You can do the attribs another way, this was convenient for
#                     the example.

def addItemAttributes(tree, idList):
    (menu, cat, subcat, item, attribs) = idList;

    if not tree.has_key(menu):  # if the menu does not exist...
        tree[menu] = {}         # Create a new dictionary for this menu
    currDict = tree[menu]       # currDict now holds the menu dictionary

    if not currDict.has_key(cat): # if the category does not exist...
        currDict[cat] = {}        # Create a new dictionary for this category
    currDict = currDict[cat]      # currDict now holds the category dictionary

    if not currDict.has_key(subcat): # if the subcategory does not exist...
        currDict[subcat] = {}        # Create a new dictionary for this subcategory
    currDict = currDict[subcat]      # currDict now holds the subcategory dictionary

    if not currDict.has_key(item): # if the category does not exist...
        currDict[item] = {}        # Create a new dictionary for this category
    currDict = currDict[item]      # currDict now holds the category dictionary

    for a in attribs
        currDict[a(0)] = a(1)

从嵌套结构中读取的函数更容易遵循:

代码语言:javascript
复制
# Understand that if any of the vaules passed to the following function
# have not been loaded, you will get an error.  This is the quick and
# dirty way.  Thank you to Janne for jarring my mind to the try/except.

def getItemAttributes(tree, menu, cat, subcat, item):
    try:
        return tree[menu][cat][subcat][item].items()
    except KeyError:
        # take care of missing keys

我希望这能帮到你。:)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9889651

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档