利用Python演示数型数据结构的教程

WBOY
Release: 2016-06-10 15:16:15
Original
1015 people have browsed it

使用 Python 内建的defaultdict 方法可以轻松定义一个树的数据结构。

简单的说树也可以是一个字典数据结构
 

def tree(): return defaultdict(tree)
Copy after login

这就是全部,就一行代码。

如果你继续下面的代码,需要先引入

from collections import defaultdict
Copy after login

实例

JSON-esque

现在我们创建一个 JSON-esque 嵌套字典无需显式的创建子字典:

users = tree()
users['harold']['username'] = 'hrldcpr'
users['handler']['username'] = 'matthandlersux'
Copy after login

然后可通过 print(json.dumps(users)) 来打印 JSON 数据,结果如下:

{"harold": {"username": "hrldcpr"}, "handler": {"username": "matthandlersux"}}
Copy after login

无需赋值

我们不需要通过赋值就可以创建结构:

taxonomy = tree()
taxonomy['Animalia']['Chordata']['Mammalia']['Carnivora']['Felidae']['Felis']['cat']
taxonomy['Animalia']['Chordata']['Mammalia']['Carnivora']['Felidae']['Panthera']['lion']
taxonomy['Animalia']['Chordata']['Mammalia']['Carnivora']['Canidae']['Canis']['dog']
taxonomy['Animalia']['Chordata']['Mammalia']['Carnivora']['Canidae']['Canis']['coyote']
taxonomy['Plantae']['Solanales']['Solanaceae']['Solanum']['tomato']
taxonomy['Plantae']['Solanales']['Solanaceae']['Solanum']['potato']
taxonomy['Plantae']['Solanales']['Convolvulaceae']['Ipomoea']['sweet potato']
Copy after login

要打印有好的信息,需要转成标准的字典对象:

def dicts(t): return {k: dicts(t[k]) for k in t}
Copy after login

现在可通过 pprint(dicts(taxonomy)) 进行打印了:

{'Animalia': {'Chordata': {'Mammalia': {'Carnivora': {'Canidae': {'Canis': {'coyote': {},
                                      'dog': {}}},
                           'Felidae': {'Felis': {'cat': {}},
                                 'Panthera': {'lion': {}}}}}}},
 'Plantae': {'Solanales': {'Convolvulaceae': {'Ipomoea': {'sweet potato': {}}},
              'Solanaceae': {'Solanum': {'potato': {},
                           'tomato': {}}}}}}
Copy after login

子结构也被当作是字典对象了,而叶子节点是一个空的字典对象

迭代

可以使用有趣的方法对树进行迭代。

例如我们解析一个动物的列表并添加到之前定义的 taxonomy 中,我们可以使用如下代码:

add(taxonomy,
  'Animalia,Chordata,Mammalia,Cetacea,Balaenopteridae,Balaenoptera,blue whale'.split(','))
Copy after login

简化实现:

def add(t, keys):
 for key in keys:
  t = t[key]
Copy after login

我们仍然无需赋值:

{'Animalia': {'Chordata': {'Mammalia': {'Carnivora': {'Canidae': {'Canis': {'coyote': {},
                                      'dog': {}}},
                           'Felidae': {'Felis': {'cat': {}},
                                 'Panthera': {'lion': {}}}},
                    'Cetacea': {'Balaenopteridae': {'Balaenoptera': {'blue whale': {}}}}}}},
 'Plantae': {'Solanales': {'Convolvulaceae': {'Ipomoea': {'sweet potato': {}}},
              'Solanaceae': {'Solanum': {'potato': {},
                           'tomato': {}}}}}}
Copy after login

结论

上面提及的这些可能用处不大,只是做了一些有意思的代码。

如果你喜欢 Python 的话,把这个当成是乐趣来理解。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!