Sujet : Re: How to enter multiple, similar, dictionaries?
De : ram (at) *nospam* zedat.fu-berlin.de (Stefan Ram)
Groupes : comp.lang.pythonDate : 11. Dec 2023, 16:54:43
Autres entêtes
Organisation : Stefan Ram
Message-ID : <codee-20231211165349@ram.dialup.fu-berlin.de>
References : 1
Chris Green <
cl@isbd.net> writes:
Is there a way to abbreviate the following code somehow?
lv = {'dev':'bbb', 'input':'1', 'name':'Leisure volts'}
sv = {'dev':'bbb', 'input':'0', 'name':'Starter volts'}
la = {'dev':'bbb', 'input':'2', 'name':'Leisure Amps'}
sa = {'dev':'bbb', 'input':'3', 'name':'Starter Amps'}
bv = {'dev':'adc2', 'input':0, 'name':'BowProp Volts'}
s = \
[ 'dev', 'input', 'name',
'lv', b:='bbb', '1', ( l:='Leisure', v:=' volts', l + v )[ -1 ],
'sv', b, '0', ( s:='Starter', s + v )[ -1 ],
'la', b, '2', ( a:=' Amps', l + a )[ -1 ],
'sa', b, '3', s + a,
'bv', 'adc2', 0, 'BowProp' + v.title() ]
for name in s[ 3:: 4 ]:
globals()[ name ]= dict()
for i in range( 4, len( s )):
key_offset = i % 4
if 3 == key_offset: continue
name = s[ i // 4 * 4 - 1 ]
key = s[ key_offset ]
value = s[ i ]
globals()[ name ][ key ]= value