I'm trying to create hierarchy lists python in python. For example, There are several states. In each state there are several counties, in each county they are several cities. Then I would like be able to call those. I've tried creating list and appending lists to those list but I can't get to that to work. It also gets really messy. Thanks
我正在尝试在python中创建层次结构列表python。例如,有几个州。在每个州都有几个县,每个县都有几个城市。然后我想能够打电话给那些人。我已经尝试创建列表并将列表附加到这些列表但我无法实现这一点。它也变得非常混乱。谢谢
I just found this example:
我刚刚找到了这个例子:
tree = [
['Food', [
['Fruit', [
['Red', ['Cherry', 'Strawberry']],
['Yellow', ['Banana']],
]],
['Meat', [
['Beef', 'Pork']
]],
]],
]
Now my question would just be, is this the best way? how to call specific things? If I use...
现在我的问题就是,这是最好的方法吗?怎么称呼特定的东西?如果我用...
print tree[0]
it will give me everything. When I try
它会给我一切。当我尝试
print tree[0[1]]
I get a type TypeError: 'int" object has no attribute '__getitem_' Thanks
我得到一个类型TypeError:'int'对象没有属性'__getitem_'谢谢
2 个解决方案
#1
1
Dictionaries are the way to go.
字典是要走的路。
d = {'food':
{'fruit':
{'color':
{'red': ['Cherry', 'Strawberry']},
{'yellow': ['Banana']},
},
},
{'meat': ['Beef', 'Pork']}
}
Now you can do:
现在你可以这样做:
for item in d['food']['fruit']['color']['red']:
print(item)
# Cherry
# Strawberry
#2
1
The direct answer to your question is that the list was likely created correctly, but you are not accessing it correctly.
您问题的直接答案是列表可能已正确创建,但您没有正确访问它。
You access the nested lists like this:
您可以像这样访问嵌套列表:
tree[0][1]...
not like this
不喜欢这个
tree[0[1[..]]]
However, you should use a dictionary, not a list. It will make your look ups much more simple and they will be more efficient as well.
但是,您应该使用字典而不是列表。它将使您的查找更加简单,并且它们也将更加高效。
If you are interested in finding out how the dictionaries work. Google the term 'Associative array'. They are also known as hashes or hash tables.
如果您有兴趣了解字典的工作原理。谷歌的术语'关联数组'。它们也称为哈希或哈希表。
Also, rather than using the term hierarchical the term multi-dimensional is more common.
而且,不是使用术语分层,而是多维一词更常见。
e.g. this:
例如这个:
mylist=[
[...],
[...],
.
.
]
Is a 2D list
是2D列表
#1
1
Dictionaries are the way to go.
字典是要走的路。
d = {'food':
{'fruit':
{'color':
{'red': ['Cherry', 'Strawberry']},
{'yellow': ['Banana']},
},
},
{'meat': ['Beef', 'Pork']}
}
Now you can do:
现在你可以这样做:
for item in d['food']['fruit']['color']['red']:
print(item)
# Cherry
# Strawberry
#2
1
The direct answer to your question is that the list was likely created correctly, but you are not accessing it correctly.
您问题的直接答案是列表可能已正确创建,但您没有正确访问它。
You access the nested lists like this:
您可以像这样访问嵌套列表:
tree[0][1]...
not like this
不喜欢这个
tree[0[1[..]]]
However, you should use a dictionary, not a list. It will make your look ups much more simple and they will be more efficient as well.
但是,您应该使用字典而不是列表。它将使您的查找更加简单,并且它们也将更加高效。
If you are interested in finding out how the dictionaries work. Google the term 'Associative array'. They are also known as hashes or hash tables.
如果您有兴趣了解字典的工作原理。谷歌的术语'关联数组'。它们也称为哈希或哈希表。
Also, rather than using the term hierarchical the term multi-dimensional is more common.
而且,不是使用术语分层,而是多维一词更常见。
e.g. this:
例如这个:
mylist=[
[...],
[...],
.
.
]
Is a 2D list
是2D列表