python - Using two dictionary keys (key 1 _and_ 2) to determine value -
i have python script read 2 bytes out of preexisting binary. 1 byte determines device type , next determines sub-device type. both defines overall device.
for instance:
x41 , x01 = printer x43 , x01 = audio device
my code need find both x41 , x01 there printer, instance.
i thought doing dictionary, think means 2 keys per value , doesn't quite trivial implement (at least skillset).
is dictionary approach? or else better?
like said in comments, 2 approachs possible:
devices={(0x41,0x01) : 'printer' , (0x43,0x01) : 'audio device', ...}
or
computerdevices={ 0x41 : 'printer' , 0x43 : 'audio device', ...} kitchendevices={ 0x41 : 'roaster' , 0x26 : 'oven', ...} ... devices = {0x01: computerdevices , 0x02 :kitchendevices, ...}
you can concatenate bytes : key =bytes([0x43,0x01])
, use dictionary key.
Comments
Post a Comment