将 OrderedDict 转换为在 python 中设置

Convert OrderedDict to set in python

我有一个这样的 OrderedDict:

OrderedDict([
    (1666, [
        ['2ZNV', 'EDO1', 14, '2FCN', 'CD', 119],
        ['2ZNV', 'EDO', 14, '2FCN', 'CD', 119]]), 
    (1056, [['2ZNV', 'EDO', 32, '2FCN','CD', 33]]), 
    (266, [['2ZNV', 'EDO', 14, '2FCN', 'CD', 19]]), 
    (171, [
        ['2ZNV', 'ZN', 9, '2FCN', 'DVA', 19], 
        ['2ZNV', 'ZN', 9, '2FCN', 'CD', 19], 
        ['2ZNV', 'ZN', 9, '2FCN', 'ACT', 19], 
        ['2ZNV', 'EDO', 9, '2FCN', 'CD', 19], 
        ['2ZNV', 'EDO', 9, '2FCN', 'DVA', 19]]),
    (45, [
        ['2ZNV','EDO', 9, '2X8L', 'GOL', 5], 
        ['2ZNV', 'ZN', 9, '2X8L', 'GOL', 5]]),
    (6, [['2ZNV', 'EDO', 2, '2FCN', 'CD', 3]]), 
    (1, [['2ZNV', 'EDO', 1, '2FCN', 'CD', 1]])
])

是否可以将 set 中的所有值相加但保持顺序?

我尝试转换为列表,然后从元组转换为这样设置:

for k,v in od.items():
    listset.append(v)
flat = [item for sublist in listset for item in sublist]
xx = set(tuple(x) for x in flat)
print xx

但这不是保持顺序。上面的结果是:

set([
    ('2ZNV', 'ZN', 9, '2FCN', 'DVA', 19),
    ('2ZNV', 'EDO', 14, '2FCN', 'CD', 119),
    ('2ZNV', 'EDO', 9, '2X8L', 'GOL', 5),
    ('2ZNV', 'EDO', 32, '2FCN', 'CD', 33),
    ('2ZNV', 'EDO', 9, '2FCN', 'DVA', 19),
    ('2ZNV', 'ZN', 9, '2FCN', 'ACT', 19),
    ('2ZNV', 'EDO', 9, '2FCN', 'CD', 19),
    ('2ZNV', 'EDO', 1, '2FCN', 'CD', 1),
    ('2ZNV', 'EDO', 14, '2FCN', 'CD', 19),
    ('2ZNV', 'ZN', 9, '2X8L', 'GOL', 5),
    ('2ZNV', 'EDO1', 14, '2FCN', 'CD', 119),
    ('2ZNV', 'EDO', 2, '2FCN', 'CD', 3),
    ('2ZNV', 'ZN', 9,'2FCN', 'CD', 19)
])

提前致谢。

您可以使用此秘籍中的 OrderedSet https://code.activestate.com/recipes/576694/。刚刚

OrderedSet([tuple(item) for sublist in od.values() for item in sublist])

集合是无序的,但您可以创建一个唯一元组列表:

listset = []
for k, v in od.items():
    if (k, v) not in listset:
        listset.append((k, v))