【问题标题】:Construction of 5 level tree structure in pythonpython中5级树结构的构建
【发布时间】:2013-12-09 00:07:41
【问题描述】:

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

我是python的初学者,所以请在创建树形结构方面帮助和建议我。

【问题讨论】:

  • 这是一个非常开放的问题。您是否希望将整个数据库拉入树中?将最近的查找存储在树中?有多种树数据结构。你打算用哪一个?为什么是5级?在二叉树中,只有 31 个值。
  • 此链接可能有帮助:stackoverflow.com/questions/2358045/…
  • 嗨 JerseyMike,我正在尝试将整个数据库拉入树中。你有什么想法或更好的解决方案吗,我有点困惑......
  • 好的,数据库中有多少条目,记录中有哪些数据,如何识别特定记录?在选择正确的数据结构时,这些事情很重要。
  • 没有看到您的数据库或使用该数据库的代码,很难为您提供帮助。但一般来说,如果您要进行大量小查询,则可以通过将小查询合并为一个大查询来获得很大的性能提升。

标签: python tree


【解决方案1】:

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


JerseyMike 给出了一个很好的例子,我只想指出他的 addItemAttributes 函数相当于更简洁
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 包装在 try 块中,以便您可以处理缺少其中一个键的情况,例如。

try:
    getItemAttributes(...)
except KeyError:
    #key was incorrect, deal with the situation

【讨论】:

  • 嗨 JerseyMike,我在字典中没有数据我在列表中有元组形式的数据。所以我该如何构造一棵树
  • @shiva 在您的问题中添加一些示例数据。
  • @shiva,是的,请给我们一个从菜单到 item_attribute 的元组示例。字典就是你将要产生的东西。我们意识到您不是从字典中的数据开始的。
  • @Janne,我喜欢您对 addItemAttributes 的整合。我打算将它实现为“currDict = reduce(lambda tr, dt: tr.setdefault(dt, {}), idList[:-1])”。这使得该函数能够处理任何深度。但是,我不想将 shiva 扔进 Python 的深处。 ;)
【解决方案2】:

因为我手头没有 Python 解释器,所以我将它们放在一起。这里有两个用于填充和读取嵌套字典结构的函数。传递给每个参数的第一个参数是包含所有菜单信息的基本字典。

此函数用于添加字典。这个函数在代码方面可能更有效,但我希望你了解发生了什么。对于每个菜单类别的每个子类别的每个项目,您都需要构造一个要传入的属性列表。

# 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)

从嵌套结构中读取的函数更容易理解:

# 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

我希望这会有所帮助。 :)

【讨论】:

    猜你喜欢
    • 2018-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-05
    相关资源
    最近更新 更多