Python 根据添加到数组中的输入文本输出值
Python to output value based from input text which is added into arrays
我是 python 编码的初学者,但我希望根据几个因素找到输出。我会尽力解释。
有6个区域ZONE = ["RED", "GREEN", "BLUE", "YELLOW", "PINK", "WHITE"]
为了争论,目前有两个设备,但还会有更多,这些设备称为 PIR 和 SMOKE
当一个新设备上线时,我 python 为它分配一个新的 ZONE 颜色,但是如果 ZONE 已经包含相同类型的设备,它应该选择另一个 ZONE 除非所有 ZONE 都已经有相同的设备然后它可以开始加倍等等。
当每个设备上线时,我收集设备详细信息,例如 MAC 地址和设备类型...数组的索引是一个设备
MAC = ["ABC","DEF","GHI"]
TYPE = ["PIR","PIR","SMOKE"]
ZONE = ["RED","GREEN","RED"]
当 4 号设备上线时,我希望 python 告诉我下一个可用的 ZONE 颜色是什么。因为我已经在 ZONE RED 和 GREEN 中有两个 PIR,我希望 PIR 设备的下一个结果是 BLUE,下一个 SMOKE 设备是 GREEN 。 Python 然后会将 ZONE 颜色发送到新设备,以便设备可以设置其区域颜色模式(前面的通信已经正常工作)。
我已经部分地能够让一个测试项目工作,但它只是告诉我下一个可用的颜色,但没有考虑设备类型。我的例子如下:
ZONE = ["RED", "GREEN", "BLUE", "YELLOW", "PINK", "WHITE"]
ZONEDevices = [0,0,0,0,0,0]
DEVICES = ["PIR", "PIR", "SMOKE"]
MAC = ["123", "456", "789"]
DEVICEZONE = ["RED", "GREEN", "RED"]
# Following counts how many devices are in each zone and adds to the array
ZONEDevices[0] = DEVICEZONE.count("RED")
ZONEDevices[1] = DEVICEZONE.count("GREEN")
ZONEDevices[2] = DEVICEZONE.count("BLUE")
ZONEDevices[3] = DEVICEZONE.count("YELLOW")
ZONEDevices[4] = DEVICEZONE.count("PURPLE")
ZONEDevices[5] = DEVICEZONE.count("WHITE")
IN = "PIR-ABC" # This is what goes into the python script
OUT = IN.split("-") # Splits the input into an array
print(OUT) # OUTPUT: ['PIR', '123']
ID = ZONEDevices.index( min(ZONEDevices) ) # OUTPUT HERE IS 2, ZONE-BLUE
if (ID == 0): print("ZONE-RED")
if (ID == 1): print("ZONE-GREEN")
if (ID == 2): print("ZONE-BLUE")
if (ID == 3): print("ZONE-YELLOW")
if (ID == 4): print("ZONE-PURPLE")
if (ID == 5): print("ZONE-WHITE")
DEVICES.append(OUT[0]) # adds the input device type to the devices array
MAC.append(OUT[1]) # adds the input device MAC to the MAC array
print(DEVICES,MAC) # OUTPUT: ['PIR', 'PIR', 'SMOKE', 'PIR'] ['123', '456', '789', 'ABC']
#but what I am wanting to achieve is to get the script to give me the next
#available zone to use if a device is not already active in that zone
#DEVICEZONE.append( UNKNOWN )
我希望这是有道理的。我不太确定从哪里开始。任何帮助将不胜感激。
你所说的问题很简单,这让我觉得我遗漏了什么。此代码将告诉您此设备类型的下一个颜色。谨慎的编码人员可能会添加 % len(ZONE)
以避免溢出:
ZONE = ["RED", "GREEN", "BLUE", "YELLOW", "PINK", "WHITE"]
DEVICES = ["PIR", "PIR", "SMOKE"]
MAC = ["123", "456", "789"]
DEVICEZONE = ["RED", "GREEN", "RED"]
def findNextColor( device ):
k = DEVICES.count(device) % len(ZONE)
return ZONE[k]
print( "Next PIR is", findNextColor('PIR') )
print( "Next SMOKE is", findNextColor('SMOKE') )
输出:
Next PIR is BLUE
Next SMOKE is GREEN
跟进
好的,所以规范不是“按顺序分配颜色”。它是“分配到目前为止命中最少的颜色,如果有平局则使用列表顺序”。请注意,我建立了一个数据库来计算每个设备的出现次数。我可能会把所有这些都放在一个 class 中,它可以在每次添加内容时更新数据库。
像这样:
ZONE = ["RED", "GREEN", "BLUE", "YELLOW", "PINK", "WHITE"]
DEVICES = ["PIR", "PIR", "PIR", "PIR","PIR","SMOKE"]
MAC = ["123", "456", "789"]
DEVICEZONE = ["RED", "GREEN", "BLUE", "YELLOW", "PINK","GREEN"]
def distribute( devs, devzones ):
z1 = { z:0 for z in ZONE }
tracker = {}
for d,z in zip(devs,devzones):
if d not in tracker:
tracker[d] = z1.copy()
tracker[d][z] += 1
return tracker
DB = distribute( DEVICES, DEVICEZONE )
print(DB)
def findNextColor( device ):
low = min( DB[device].values() )
return [k for k,v in DB[device].items() if v == low][0]
print( "Next PIR is", findNextColor('PIR') )
print( "Next SMOKE is", findNextColor('SMOKE') )
输出:
Next PIR is WHITE
Next SMOKE is RED
我是 python 编码的初学者,但我希望根据几个因素找到输出。我会尽力解释。
有6个区域ZONE = ["RED", "GREEN", "BLUE", "YELLOW", "PINK", "WHITE"]
为了争论,目前有两个设备,但还会有更多,这些设备称为 PIR 和 SMOKE
当一个新设备上线时,我 python 为它分配一个新的 ZONE 颜色,但是如果 ZONE 已经包含相同类型的设备,它应该选择另一个 ZONE 除非所有 ZONE 都已经有相同的设备然后它可以开始加倍等等。
当每个设备上线时,我收集设备详细信息,例如 MAC 地址和设备类型...数组的索引是一个设备
MAC = ["ABC","DEF","GHI"]
TYPE = ["PIR","PIR","SMOKE"]
ZONE = ["RED","GREEN","RED"]
当 4 号设备上线时,我希望 python 告诉我下一个可用的 ZONE 颜色是什么。因为我已经在 ZONE RED 和 GREEN 中有两个 PIR,我希望 PIR 设备的下一个结果是 BLUE,下一个 SMOKE 设备是 GREEN 。 Python 然后会将 ZONE 颜色发送到新设备,以便设备可以设置其区域颜色模式(前面的通信已经正常工作)。
我已经部分地能够让一个测试项目工作,但它只是告诉我下一个可用的颜色,但没有考虑设备类型。我的例子如下:
ZONE = ["RED", "GREEN", "BLUE", "YELLOW", "PINK", "WHITE"]
ZONEDevices = [0,0,0,0,0,0]
DEVICES = ["PIR", "PIR", "SMOKE"]
MAC = ["123", "456", "789"]
DEVICEZONE = ["RED", "GREEN", "RED"]
# Following counts how many devices are in each zone and adds to the array
ZONEDevices[0] = DEVICEZONE.count("RED")
ZONEDevices[1] = DEVICEZONE.count("GREEN")
ZONEDevices[2] = DEVICEZONE.count("BLUE")
ZONEDevices[3] = DEVICEZONE.count("YELLOW")
ZONEDevices[4] = DEVICEZONE.count("PURPLE")
ZONEDevices[5] = DEVICEZONE.count("WHITE")
IN = "PIR-ABC" # This is what goes into the python script
OUT = IN.split("-") # Splits the input into an array
print(OUT) # OUTPUT: ['PIR', '123']
ID = ZONEDevices.index( min(ZONEDevices) ) # OUTPUT HERE IS 2, ZONE-BLUE
if (ID == 0): print("ZONE-RED")
if (ID == 1): print("ZONE-GREEN")
if (ID == 2): print("ZONE-BLUE")
if (ID == 3): print("ZONE-YELLOW")
if (ID == 4): print("ZONE-PURPLE")
if (ID == 5): print("ZONE-WHITE")
DEVICES.append(OUT[0]) # adds the input device type to the devices array
MAC.append(OUT[1]) # adds the input device MAC to the MAC array
print(DEVICES,MAC) # OUTPUT: ['PIR', 'PIR', 'SMOKE', 'PIR'] ['123', '456', '789', 'ABC']
#but what I am wanting to achieve is to get the script to give me the next
#available zone to use if a device is not already active in that zone
#DEVICEZONE.append( UNKNOWN )
我希望这是有道理的。我不太确定从哪里开始。任何帮助将不胜感激。
你所说的问题很简单,这让我觉得我遗漏了什么。此代码将告诉您此设备类型的下一个颜色。谨慎的编码人员可能会添加 % len(ZONE)
以避免溢出:
ZONE = ["RED", "GREEN", "BLUE", "YELLOW", "PINK", "WHITE"]
DEVICES = ["PIR", "PIR", "SMOKE"]
MAC = ["123", "456", "789"]
DEVICEZONE = ["RED", "GREEN", "RED"]
def findNextColor( device ):
k = DEVICES.count(device) % len(ZONE)
return ZONE[k]
print( "Next PIR is", findNextColor('PIR') )
print( "Next SMOKE is", findNextColor('SMOKE') )
输出:
Next PIR is BLUE
Next SMOKE is GREEN
跟进
好的,所以规范不是“按顺序分配颜色”。它是“分配到目前为止命中最少的颜色,如果有平局则使用列表顺序”。请注意,我建立了一个数据库来计算每个设备的出现次数。我可能会把所有这些都放在一个 class 中,它可以在每次添加内容时更新数据库。
像这样:
ZONE = ["RED", "GREEN", "BLUE", "YELLOW", "PINK", "WHITE"]
DEVICES = ["PIR", "PIR", "PIR", "PIR","PIR","SMOKE"]
MAC = ["123", "456", "789"]
DEVICEZONE = ["RED", "GREEN", "BLUE", "YELLOW", "PINK","GREEN"]
def distribute( devs, devzones ):
z1 = { z:0 for z in ZONE }
tracker = {}
for d,z in zip(devs,devzones):
if d not in tracker:
tracker[d] = z1.copy()
tracker[d][z] += 1
return tracker
DB = distribute( DEVICES, DEVICEZONE )
print(DB)
def findNextColor( device ):
low = min( DB[device].values() )
return [k for k,v in DB[device].items() if v == low][0]
print( "Next PIR is", findNextColor('PIR') )
print( "Next SMOKE is", findNextColor('SMOKE') )
输出:
Next PIR is WHITE
Next SMOKE is RED