如何在有序字典中获取正确数量的子元素?
How to get correct number of sub elements in ordered dictionary?
我有以下 2 个相似的有序字典(它们是前面步骤的输出),唯一的区别是 dict1
只有一个节点名为 GROUP
和 dict2
有两个(如图中的 0 和 1)
from collections import OrderedDict
dict1 = OrderedDict([('CATALOG',
OrderedDict([('GROUP',
OrderedDict([
('ZONE', '4'),
('LIGHT', 'Mostly Shady'),
('PLANT', [
OrderedDict([
('COMMON', 'Bloodroot'),
('BOTANICAL', 'Sanguinaria canadensis')]),
OrderedDict([
('COMMON', 'Columbine'),
('BOTANICAL', 'Aquilegia canadensis')])])]))]))])
dict2 = OrderedDict([('CATALOG',
OrderedDict([('GROUP', [
OrderedDict([
('ZONE', '3'),
('LIGHT', 'Mostly Shady'),
('PLANT', [
OrderedDict([
('COMMON', "Dutchman's-Breeches"),
('BOTANICAL', 'Dicentra cucullaria')]),
OrderedDict([
('COMMON', 'Ginger, Wild'),
('BOTANICAL', 'Asarum canadense')])])]),
OrderedDict([
('ZONE', '4'),
('LIGHT', 'Mostly Sunny'),
('PLANT', [
OrderedDict([
('COMMON', 'Marsh Marigold'),
('BOTANICAL', 'Caltha palustris')]),
OrderedDict([
('COMMON', 'Cowslip'),
('BOTANICAL', 'Caltha palustris')])])])])]))])
其中的结构是这样的:
我在确定每个字典有多少组时遇到问题,我的尝试如下所示,显示 dict2
的正确组数为 2,但 dict1
显示为答案 3当只有一组时。如果我使用循环打印每个组,则 dict2
打印 2 个正确的 orderedDict,但 dict1
打印其他不是 GROUP
的子节点。我做错了什么。
>>> len(dict1['CATALOG']['GROUP'])
3
>>> len(dict2['CATALOG']['GROUP'])
2
>>> dict1['CATALOG']['GROUP'][0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 0
>>>
>>> dict2['CATALOG']['GROUP'][0]
OrderedDict([('ZONE', '3'), ('LIGHT', 'Mostly Shady')...
>>>
>>> dict2['CATALOG']['GROUP'][1] ...
OrderedDict([('ZONE', '4'), ('LIGHT', 'Mostly Sunny')...
>>> ...
>>> for group in dict1['CATALOG']['GROUP']:
... print(group)
...
ZONE
LIGHT
PLANT
>>> for group in dict2['CATALOG']['GROUP']:
... print(group)
...
OrderedDict([('ZONE', '3'), ('LIGHT', 'Mostly Shady')...
OrderedDict([('ZONE', '4'), ('LIGHT', 'Mostly Sunny')...
检查dictX['CATALOG']['GROUP']
是否是一个列表。如果不是列表,则长度为1。
group1_len = len(dict1['CATALOG']['GROUP']) if isinstance(dict1['CATALOG']['GROUP'], list) else 1
group2_len = len(dict2['CATALOG']['GROUP']) if isinstance(dict2['CATALOG']['GROUP'], list) else 1
如果您先将字典包装在列表中,则可能会进行所有进一步处理。那么两个词典将具有相似的结构,您将不需要继续使用条件。
for d in (dict1, dict2):
if not isinstance(d['CATALOG']['GROUP'], list):
d['CATALOG']['GROUP'] = [d['CATALOG']['GROUP']]
您为 dict1
获得的长度就是密钥本身。如果我们知道这个对象是 Dict
类型,我们可以使用辅助方法做一些工作。
您可以在开始时进行一些类型检查以确定对象的形状。这会告诉您基础形状是 Dict
(也就是 1 项)还是 List
(也就是多个)。
# something with 1 item, dict rendering
dict1 = {...}
# something with 2 items, list rendering
dict2 = {...}
# helper method
def checkShape(dict):
length = 0
if isinstance(dict['CATALOG']['GROUP'], dict):
# we know len is 1
length = 1
else:
# we know len is >1
length = len(dict['CATALOG']['GROUP'])
return length
我有以下 2 个相似的有序字典(它们是前面步骤的输出),唯一的区别是 dict1
只有一个节点名为 GROUP
和 dict2
有两个(如图中的 0 和 1)
from collections import OrderedDict
dict1 = OrderedDict([('CATALOG',
OrderedDict([('GROUP',
OrderedDict([
('ZONE', '4'),
('LIGHT', 'Mostly Shady'),
('PLANT', [
OrderedDict([
('COMMON', 'Bloodroot'),
('BOTANICAL', 'Sanguinaria canadensis')]),
OrderedDict([
('COMMON', 'Columbine'),
('BOTANICAL', 'Aquilegia canadensis')])])]))]))])
dict2 = OrderedDict([('CATALOG',
OrderedDict([('GROUP', [
OrderedDict([
('ZONE', '3'),
('LIGHT', 'Mostly Shady'),
('PLANT', [
OrderedDict([
('COMMON', "Dutchman's-Breeches"),
('BOTANICAL', 'Dicentra cucullaria')]),
OrderedDict([
('COMMON', 'Ginger, Wild'),
('BOTANICAL', 'Asarum canadense')])])]),
OrderedDict([
('ZONE', '4'),
('LIGHT', 'Mostly Sunny'),
('PLANT', [
OrderedDict([
('COMMON', 'Marsh Marigold'),
('BOTANICAL', 'Caltha palustris')]),
OrderedDict([
('COMMON', 'Cowslip'),
('BOTANICAL', 'Caltha palustris')])])])])]))])
其中的结构是这样的:
我在确定每个字典有多少组时遇到问题,我的尝试如下所示,显示 dict2
的正确组数为 2,但 dict1
显示为答案 3当只有一组时。如果我使用循环打印每个组,则 dict2
打印 2 个正确的 orderedDict,但 dict1
打印其他不是 GROUP
的子节点。我做错了什么。
>>> len(dict1['CATALOG']['GROUP'])
3
>>> len(dict2['CATALOG']['GROUP'])
2
>>> dict1['CATALOG']['GROUP'][0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 0
>>>
>>> dict2['CATALOG']['GROUP'][0]
OrderedDict([('ZONE', '3'), ('LIGHT', 'Mostly Shady')...
>>>
>>> dict2['CATALOG']['GROUP'][1] ...
OrderedDict([('ZONE', '4'), ('LIGHT', 'Mostly Sunny')...
>>> ...
>>> for group in dict1['CATALOG']['GROUP']:
... print(group)
...
ZONE
LIGHT
PLANT
>>> for group in dict2['CATALOG']['GROUP']:
... print(group)
...
OrderedDict([('ZONE', '3'), ('LIGHT', 'Mostly Shady')...
OrderedDict([('ZONE', '4'), ('LIGHT', 'Mostly Sunny')...
检查dictX['CATALOG']['GROUP']
是否是一个列表。如果不是列表,则长度为1。
group1_len = len(dict1['CATALOG']['GROUP']) if isinstance(dict1['CATALOG']['GROUP'], list) else 1
group2_len = len(dict2['CATALOG']['GROUP']) if isinstance(dict2['CATALOG']['GROUP'], list) else 1
如果您先将字典包装在列表中,则可能会进行所有进一步处理。那么两个词典将具有相似的结构,您将不需要继续使用条件。
for d in (dict1, dict2):
if not isinstance(d['CATALOG']['GROUP'], list):
d['CATALOG']['GROUP'] = [d['CATALOG']['GROUP']]
您为 dict1
获得的长度就是密钥本身。如果我们知道这个对象是 Dict
类型,我们可以使用辅助方法做一些工作。
您可以在开始时进行一些类型检查以确定对象的形状。这会告诉您基础形状是 Dict
(也就是 1 项)还是 List
(也就是多个)。
# something with 1 item, dict rendering
dict1 = {...}
# something with 2 items, list rendering
dict2 = {...}
# helper method
def checkShape(dict):
length = 0
if isinstance(dict['CATALOG']['GROUP'], dict):
# we know len is 1
length = 1
else:
# we know len is >1
length = len(dict['CATALOG']['GROUP'])
return length