- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 OrderedDict,我正在将其传递给函数。在函数的某个地方它改变了顺序,尽管我不确定为什么并且正在尝试调试它。以下是该函数以及函数和输出的示例:
def unnest_data(data):
path_prefix = ''
UNNESTED = OrderedDict()
list_of_subdata = [(data, ''),] # (data, prefix)
while list_of_subdata:
for subdata, path_prefix in list_of_subdata:
for key, value in subdata.items():
path = (path_prefix + '.' + key).lstrip('.').replace('.[', '[')
if not (isinstance(value, (list, dict))):
UNNESTED[path] = value
elif isinstance(value, dict):
list_of_subdata.append((value, path))
elif isinstance(value, list):
list_of_subdata.extend([(_, path) for _ in value])
list_of_subdata.remove((subdata, path_prefix))
if not list_of_subdata: break
return UNNESTED
然后,如果我调用它:
from collections import OrderedDict
data = OrderedDict([('Item', OrderedDict([('[@ID]', '288917'), ('Main', OrderedDict([('Platform', 'iTunes'), ('PlatformID', '353736518')])), ('Genres', OrderedDict([('Genre', [OrderedDict([('[@FacebookID]', '6003161475030'), ('Value', 'Comedy')]), OrderedDict([('[@FacebookID]', '6003172932634'), ('Value', 'TV-Show')])])]))]))])
unnest_data(data)
我得到的 OrderedDict 与原来的顺序不匹配:
OrderedDict([('Item[@ID]', '288917'), ('Item.Genres.Genre[@FacebookID]', ['6003172932634', '6003161475030']), ('Item.Genres.Genre.Value', ['TV-Show', 'Comedy']), ('Item.Main.Platform', 'iTunes'), ('Item.Main.PlatformID', '353736518')])
注意它在“PlatformID”之前有“Genre”,这不是它在原始字典中的排序方式。我的错误似乎是什么?我该如何修复它?
最佳答案
如果没有完整的工作示例,很难准确说出问题所在。但根据您所显示的代码,我怀疑您的问题根本不在于 OrderedDict
,而是您在迭代时修改 list_of_subdata
,这将导致项目被意外跳过。
>>> a = [1, 2, 3, 4, 5, 6, 7]
>>> for x in a:
... print(x)
... a.remove(x)
...
1
3
5
7
根据您的用途,请考虑 deque
而不是列表。
关于python - 保持 OrderedDict 的顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53991869/
我现在很迷茫。我正在使用 PyYAML 编辑一些 YAML 文件。 data = yaml.load_all(open('testingyaml.yaml'),Loader=yaml.RoundTri
嗨,我想解析一些 order_dicts。我有以下错误 ipdb> for elem in my_orderdict['codeBook'].keys(): print("{0}
我正在尝试通过 'depth' 键对 OrderedDict 中的 OrderedDict 进行排序。有什么解决方案可以对该 Dictionary 进行排序吗? OrderedDict([ (2,
我试图确定不同 numpy orderdict 对象中保存的两个值是否相同。 这两个字典都是通过使用 turbodbc 中的 fetchallnumpy() 选项创建的,并且包含两个键。第一个键是 i
我有两个有序的字典 D1 和 D2。我想将 D2 的键名分配给 D1(覆盖 D1 的现有键名)。如何做到这一点? 例子: D1 = {'first_key': 10, 'second_key': 20
我有一个看起来像是一个 OrderdDict 的东西,其中包含我所指的“主”OrderedDict 的子集中的 OrderedDict。我正在尝试将此对象解析为 Pandas DataFrame。 (
我正在尝试向 mysql 中插入一个有序的字典,但我认为我遗漏了一个简单的项目。 sql_command = """CREATE TABLE asdf ( id INTEGER PRIMARY KEY
我有一个OrderedDict,我需要在保持排序的同时添加一个元素 import sys import bisect from collections import OrderedDict arr =
我有一个 python OrderedDict,当我只更新一个键值时,所有其他键值对也会更新。我已经包含了下面的源代码和跟踪。 我期望有一个 key 对 (2014, {'start': 2014,
import collections d = collections.defaultdict(dict) d["i"]["a"] = "111" d["i"]["b"] = "222" print d
在脚本中,我有一个 OrderedDict groups,它按字母顺序获取键/值对。 在脚本的另一部分,我正在检查与 key 相同的文件,如下所示: for (key, value) in group
想法是这样的:我有一个像这样的 orderedDict(简化版): {'012013': 3, '022013': 1, '032013': 5} 我想做的是通过以某种方式迭代它来累积所有值。例如,我
这个循环的想法是遍历一个列表。如果对象的某个属性不是 OrderedDict 的键,它将添加它。它是对象列表的字典 for object in someList: if object.DATE n
我正在尝试找出如何打印一个列表 storeItems,其中包含 3 个 OrderedDict(),并且能够打印 key 和每个 OrderedDict 的 value。我不知道如何解决这个问题,这就
我以为我为自己设定了一个简单的项目,但我想不是。我认为我长期使用 Ordered dict 函数是因为我不断得到: ValueError: too many values to unpack (ex
OrderedDict([(u'attributes', OrderedDict([(u'type', u'Campaign__c'), (u'url', u'/services/data/v29.0
我有这个代码: self.statusIcons = collections.OrderedDict for index in guiConfig.STATUS_ICON_SETS: sel
我有以下相当复杂的数据结构: temp_dict = { 'a': { 'aardvark': (6,True), 'apple': (3,True)
OrderedDict 能否获得关键位置? 类似于 index() 的 list test = ['a', 'b', 'c', 'd', 'e'] test.index('b') # return 1
我在回答关于 SO 的其中一个问题时偶然发现了 OrderedDict 的这种行为,我无法解释。它进行如下你有一个看起来像这样的字典: exmpl = OrderedDict([(30, ('A1'
我是一名优秀的程序员,十分优秀!