Thursday, 15 May 2014

Iterate through triple nested tuple Python 3.4 -



Iterate through triple nested tuple Python 3.4 -

let's have triple nested tuple using create keyboard in tkinter. looks this:

kboard = ( ( ('~\n`', 1), ('!\n1', 1),..... etc. ), (('tab', 2), ('q', 1),.... etc. ), (('capslock', 2), ('a', 1),... etc. , on ), )

let's want iterate through such sec layer rows of keyboard , want grab each definition utilize key. tried nested loop , gave me multiple duplicates , didn't allow me grab individual definitions. how can grab each definition individually? maintain in mind, trying grab these definitions in order in in tuple.

try itertools.chain:-

dict(itertools.chain(*kboard))

output:-

{'a': 1, '!\n1': 1, 'q': 1, 'capslock': 2, '~\n`': 1, 'tab': 2}

or may list comprehension dict function,

>>>[dict(tup) tup in kboard]

output:-

>>> [{'!\n1': 1, '~\n`': 1}, {'q': 1, 'tab': 2}, {'a': 1, 'capslock': 2}]

or utilize simple map function:-

>>>map(dict, kboard) [{'!\n1': 1, '~\n`': 1}, {'q': 1, 'tab': 2}, {'a': 1, 'capslock': 2}]

python nested iteration tuples

No comments:

Post a Comment