实现类别树
Implementing a category tree
我正在实施 continent-country 关系类别图
在我的实现中,一个方法 add_country()
,作为 add_country
的参数,我提供了它所属的国家和大陆的名称,例如 add_country(Uganda, Africa)
。这添加了一个节点 Africa->Uganda。
如果我传递一个类似的函数,第二个参数为0
,例如。 add_country(Asia, 0)
,它应该添加第一个参数作为类别头或parent。
get_country()
方法应该 return 属于该大陆的所有国家的名称(或 children)。
到目前为止我已经做到了:
class Node:
def __init__(self, val):
self.children = []
self.value = val
def add_child(self, val):
n = Node(val)
self.children.append(n)
def print_stat(self):
print(self.children)
print(self.value)
class CountryTree:
def __init__(self):
self.roots = []
def add_country(self, country, parent):
if parent == None:
root = Node(country)
self.roots.append(root)
else:
par = Node(parent)
par.add_child(country)
def get_country(self, parent):
par = Node(parent)
return par.children
map = CountryTree()
map.add_country('Asia', None)
map.add_country('China', 'Asia')
map.add_country('Russia', 'Asia')
map.get_country('Asia') #Should return China and Russia
但是 运行 这是 return 给我一个空数组。
我认为以下内容符合您的要求。大多数重大变化是 CountryTree
class。我把你的大洲列表改成了字典。这使得确定一个人是否存在变得容易和快速——这是你的 add_country()
方法没有做的——这是它的主要错误之一。我还将其名称从 self.roots
更改为 self.continents
以更具描述性。我还为 parent
参数提供了默认值 None
,这使得在添加大洲节点时提供它是可选的。
另一个重要的变化是使代码遵循 PEP 8 - Style Guide for Python Code 指南,使其更易读和理解。我强烈建议您自己阅读并关注它们。
class Node:
def __init__(self, value):
self.children = []
self.value = value
def add_child(self, value):
self.children.append(Node(value))
def __repr__(self):
classname = type(self).__name__
return (f'{classname}({self.value!r}, {self.children})' if self.children else
f'{classname}({self.value!r})')
def print_stat(self):
print(self.children)
print(self.value)
class CountryTree:
def __init__(self):
self.continents = {} # Dictionary mapping continents to countries.
def add_country(self, country, parent=None):
if not parent: # Adding a continent?
continent = country
self.continents[continent] = Node(continent)
else: # Adding a country to a continent.
continent = self.continents.get(parent, None)
if not continent:
raise KeyError(f'No continent named {parent} exists')
continent.add_child(country)
def get_country(self, parent):
continent = self.continents.get(parent, None)
if not continent:
raise KeyError(f'No continent named {parent} exists')
return continent.children
map = CountryTree()
map.add_country('Asia')
map.add_country('China', 'Asia')
map.add_country('Russia', 'Asia')
result = map.get_country('Asia') # Should return China and Russia
print(result)
输出:
[Node('China'), Node('Russia')]
我正在实施 continent-country 关系类别图
在我的实现中,一个方法 add_country()
,作为 add_country
的参数,我提供了它所属的国家和大陆的名称,例如 add_country(Uganda, Africa)
。这添加了一个节点 Africa->Uganda。
如果我传递一个类似的函数,第二个参数为0
,例如。 add_country(Asia, 0)
,它应该添加第一个参数作为类别头或parent。
get_country()
方法应该 return 属于该大陆的所有国家的名称(或 children)。
到目前为止我已经做到了:
class Node:
def __init__(self, val):
self.children = []
self.value = val
def add_child(self, val):
n = Node(val)
self.children.append(n)
def print_stat(self):
print(self.children)
print(self.value)
class CountryTree:
def __init__(self):
self.roots = []
def add_country(self, country, parent):
if parent == None:
root = Node(country)
self.roots.append(root)
else:
par = Node(parent)
par.add_child(country)
def get_country(self, parent):
par = Node(parent)
return par.children
map = CountryTree()
map.add_country('Asia', None)
map.add_country('China', 'Asia')
map.add_country('Russia', 'Asia')
map.get_country('Asia') #Should return China and Russia
但是 运行 这是 return 给我一个空数组。
我认为以下内容符合您的要求。大多数重大变化是 CountryTree
class。我把你的大洲列表改成了字典。这使得确定一个人是否存在变得容易和快速——这是你的 add_country()
方法没有做的——这是它的主要错误之一。我还将其名称从 self.roots
更改为 self.continents
以更具描述性。我还为 parent
参数提供了默认值 None
,这使得在添加大洲节点时提供它是可选的。
另一个重要的变化是使代码遵循 PEP 8 - Style Guide for Python Code 指南,使其更易读和理解。我强烈建议您自己阅读并关注它们。
class Node:
def __init__(self, value):
self.children = []
self.value = value
def add_child(self, value):
self.children.append(Node(value))
def __repr__(self):
classname = type(self).__name__
return (f'{classname}({self.value!r}, {self.children})' if self.children else
f'{classname}({self.value!r})')
def print_stat(self):
print(self.children)
print(self.value)
class CountryTree:
def __init__(self):
self.continents = {} # Dictionary mapping continents to countries.
def add_country(self, country, parent=None):
if not parent: # Adding a continent?
continent = country
self.continents[continent] = Node(continent)
else: # Adding a country to a continent.
continent = self.continents.get(parent, None)
if not continent:
raise KeyError(f'No continent named {parent} exists')
continent.add_child(country)
def get_country(self, parent):
continent = self.continents.get(parent, None)
if not continent:
raise KeyError(f'No continent named {parent} exists')
return continent.children
map = CountryTree()
map.add_country('Asia')
map.add_country('China', 'Asia')
map.add_country('Russia', 'Asia')
result = map.get_country('Asia') # Should return China and Russia
print(result)
输出:
[Node('China'), Node('Russia')]