将值添加到 defaultdict set breaking on 128
adding values to defaultdict set breaking on 128
一旦我的地址达到值 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']]
编辑:
这是我提供输入的 运行nable 示例。
你可以直接运行这个例子(我用的是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 变量中。
编辑2:
这是显示问题的实时代码
https://repl.it/talk/share/adding-values-to-defaultdict-set-breaking-on-128/122013
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))
如果您想忽略重复,那么除了列表之外,您还必须通过创建集合来检查这些...:[=14=]
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())
一旦我的地址达到值 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']]
编辑: 这是我提供输入的 运行nable 示例。 你可以直接运行这个例子(我用的是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 变量中。
编辑2: 这是显示问题的实时代码 https://repl.it/talk/share/adding-values-to-defaultdict-set-breaking-on-128/122013
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))
如果您想忽略重复,那么除了列表之外,您还必须通过创建集合来检查这些...:[=14=]
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())