python - What would be the most optimised way to access values in a nested array whilst respecting pep8? -


i have been using nested arrays parsed json. ends giving gigantic line each time try access values in data. let's have nested array in var data, when try reach deeper values, still have respect 80 characters limit. want read or modify value.

self.data["name1"]["name2"][varwithnumber][varwithnumber2][varwithnumber3] 

now, thought 2 possible solutions use:

1- split using temporary vars , reasign data once done ex:

tempdata=self.data["name1"]["name2"][varwithnumber] tempdata[varwithnumber2][varwithnumber3]+=1 self.data["name1"]["name2"][varwithnumber]=tempdata 

i guess solution use quite bit of ressources memory copied around.

2- use exec function implemented in python , split string on multiple lines:

exec ('self.data'+       '["name1"]'+       '["name2"]'+       '[varwithnumber]'+       '[varwithnumber2]'+       '[varwithnumber3]+=1') 

i have no idea how optimised exec function. pythonic/optimised way this? there other/better way reach goal whilst respecting pep8?

(bit long comment) don't need exec that... can use line continuation operator:

self.data["name1"]\          ["name2"]\          [varwithnumber]\          [varwithnumber2]\           [varwithnumber3] 

demo:

in [635]: x = [[[[1, 2, 3]]]]  in [636]: x[0]\      ...:  [0]\      ...:  [0]\      ...:  [0] out[636]: 1 

this seems easiest , cleanest way it.

don't use exec unless have to. actually, don't use it, ever.


in cases, keeping reference sub dict works if visit part of data structure again , again. matter of deciding best solution apply given situation , circumstances.


Comments

Popular posts from this blog

html - How to set bootstrap input responsive width? -

javascript - Highchart x and y axes data from json -

javascript - Get js console.log as python variable in QWebView pyqt -