1

一旦我的地址达到值 128,总是会出现问题,并且它会停止将新值放在嵌套字典的末尾。而是重新从头开始订购。为什么要这样做?

result.setdefault(fan_definitions_name, set()).add(int(address))

实时变量值

我循环的数据结构看起来像这样->

list_address_of_selected_fans = [[101, 'standard fan definition1', 'JVEC-SP'],[102, 'standard fan definition1', 'JVEC-SP']]

编辑:这是我提供输入的可运行示例。你可以直接运行这个例子(我用的是python 3.8.2)

list_address_of_selected_fans = [[101, 'standard fan definition1', 'JVEC-SP'],
                                 [102, 'standard fan definition1', 'JVEC-SP'],
                                 [103, 'standard fan definition1', 'JVEC-SP'],
                                 [104, 'standard fan definition1', 'JVEC-SP'],
                                 [105, 'standard fan definition1', 'JVEC-SP'],
                                 [106, 'standard fan definition1', 'JVEC-SP'],
                                 [107, 'standard fan definition1', 'JVEC-SP'],
                                 [108, 'standard fan definition1', 'JVEC-SP'],
                                 [109, 'standard fan definition1', 'JVEC-SP'],
                                 [110, 'standard fan definition1', 'JVEC-SP'],
                                 [111, 'standard fan definition1', 'JVEC-SP'],
                                 [112, 'standard fan definition1', 'JVEC-SP'],
                                 [113, 'standard fan definition1', 'JVEC-SP'],
                                 [114, 'standard fan definition1', 'JVEC-SP'],
                                 [115, 'standard fan definition1', 'JVEC-SP'],
                                 [116, 'standard fan definition1', 'JVEC-SP'],
                                 [117, 'standard fan definition1', 'JVEC-SP'],
                                 [118, 'standard fan definition1', 'JVEC-SP'],
                                 [119, 'standard fan definition1', 'JVEC-SP'],
                                 [120, 'standard fan definition1', 'JVEC-SP'],
                                 [121, 'standard fan definition1', 'JVEC-SP'],
                                 [122, 'standard fan definition1', 'JVEC-SP'],
                                 [123, 'standard fan definition1', 'JVEC-SP'],
                                 [124, 'standard fan definition1', 'JVEC-SP'],
                                 [125, 'standard fan definition1', 'JVEC-SP'],
                                 [126, 'standard fan definition1', 'JVEC-SP'],
                                 [127, 'standard fan definition1', 'JVEC-SP'],
                                 [128, 'standard fan definition1', 'JVEC-SP'],
                                 [129, 'standard fan definition1', 'JVEC-SP'],
                                 [130, 'standard fan definition1', 'JVEC-SP'],
                                 [131, 'standard fan definition1', 'JVEC-SP'],
                                 [132, 'standard fan definition1', 'JVEC-SP'],
                                 [133, 'standard fan definition1', 'JVEC-SP'],
                                 [134, 'standard fan definition1', 'JVEC-SP'],
                                 [135, 'standard fan definition1', 'JVEC-SP'],
                                 [136, 'standard fan definition1', 'JVEC-SP'],
                                 [137, 'standard fan definition1', 'JVEC-SP'],
                                 [138, 'standard fan definition1', 'JVEC-SP'],
                                 [139, 'standard fan definition1', 'JVEC-SP'],
                                 [140, 'standard fan definition1', 'JVEC-SP'],
                                 [141, 'standard fan definition1', 'JVEC-SP'],
                                 [142, 'standard fan definition1', 'JVEC-SP'],
                                 [143, 'standard fan definition1', 'JVEC-SP'],
                                 [144, 'standard fan definition1', 'JVEC-SP'],
                                 [145, 'standard fan definition1', 'JVEC-SP'],
                                 [146, 'standard fan definition1', 'JVEC-SP'],
                                 [147, 'standard fan definition1', 'JVEC-SP'],
                                 [148, 'standard fan definition1', 'JVEC-SP'],
                                 [149, 'standard fan definition1', 'JVEC-SP'],
                                 [150, 'standard fan definition1', 'JVEC-SP']]
result = {}

for address, fan_definitions_name, fan_type in list_address_of_selected_fans:
    result.setdefault(fan_definitions_name, set()).add(int(address))

print(result)

结果最终是 -> {'standard fan definition1': {128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146、147、148、149、150、101、102、103、104、105、106、107、108、109、110、111、112、113、114、115、116、117、118、119、120、 121, 122, 123, 124, 125, 126, 127}}

为什么要这样做?这些数字应按顺序排列,例如它们最初位于 list_address_of_selected_fans 变量中。

EDIT2:这是显示问题的实时代码 https://repl.it/talk/share/adding-values-to-defaultdict-set-break-on-128/122013

4

1 回答 1

2

set() 对象是一个无序集合(参见https://docs.python.org/3.8/library/stdtypes.html#set-types-set-frozenset

出于您的目的,如果您希望它们按最初出现的顺序排列并且您将允许重复,您可以将 set() 和 add() 换成 list() 和 append():

for address, fan_definitions_name, fan_type in list_address_of_selected_fans:
    result.setdefault(fan_definitions_name, list()).append(int(address))

如果你想忽略重复,那么你必须通过在列表之外创建集合来检查这些......:

result = {}
result_sets = {}

for address, fan_definitions_name, fan_type in list_address_of_selected_fans:
    iaddress = int(address)
    prev_set = result_sets.get(fan_definitions_name, set())
    if (iaddress not in prev_set):
        result.setdefault(fan_definitions_name, list()).append(iaddress)
        result_sets.setdefault(fan_definitions_name, set()).add(iaddress)

或者,如果您想在一个变量中使用更少的代码,您可以使用 OrderedDict 来完成,如下所示:

from collections import OrderedDict
result = {}
for address, fan_definitions_name, fan_type in list_address_of_selected_fans:
    result.setdefault(fan_definitions_name, OrderedDict())[int(address)] = None

print(result['standard fan definition1'].keys())
于 2021-02-15T20:00:40.650 回答