# Tutotial: # https://docs.python.org/2/tutorial/datastructures.html#tut-loopidioms tests = { "test-1": { "urls": [ "appurl-1a", "appurl-1b" ], "layers": [ "flayer-1a", "flayer-1b" ] }, "test-2": { "urls": [ "appurl-2a", "appurl-2b" ], "layers": [ "flayer-2a", "flayer-2b" ] }, "test-3": { "urls": [ "appurl-3a", "appurl-3b" ], "layers": [ "flayer-3a", "flayer-3b" ] }, "test-4": { "urls": [ "appurl-4a", "appurl-4b" ], "layers": [ "flayer-4a", "flayer-4b" ] } } print "\n", "tests defined using an object:" for testName in tests: print "\n", "[ ", testName, " ]" appUrls = tests[testName]["urls"] layerUrls = tests[testName]["layers"] # One way to iterate over an array: print "Looping technique A:" for url in appUrls: print " ", url # Another way to iterate over an array: # This method is better since you get the array index # which you can use to read the corresponding element in # layerUrls. print "Looping technique B:" for index, appUrl in enumerate(appUrls): print " ", index, appUrl, layerUrls[index] # This technique works too: print "Looping technique C:" for index in range(len(appUrls)): print " ", index, appUrls[index], layerUrls[index] # This technique is even better: print "Looping technique D:" for appUrl, layerUrl in zip(appUrls, layerUrls): print " ", appUrl, layerUrl # Notice the above looping over "tests" does not list tests in the order # they are defined i.e. test-4 is processed first before 1, 2 and 3. # If you want to fix this, you need to define tests as an array instead # of object: print "\n==============================" print "\n", "tests defined using an array:" # See how the test name is now a property of each test object. tests2 = [ { "name": "test-1", "urls": [ "appurl-1a", "appurl-1b" ], "layers": [ "flayer-1a", "flayer-1b" ] }, { "name": "test-2", "urls": [ "appurl-2a", "appurl-2b" ], "layers": [ "flayer-2a", "flayer-2b" ] }, { "name": "test-3", "urls": [ "appurl-3a", "appurl-3b" ], "layers": [ "flayer-3a", "flayer-3b" ] }, { "name": "test-4", "urls": [ "appurl-4a", "appurl-4b" ], "layers": [ "flayer-4a", "flayer-4b" ] } ] for testObj in tests2: print "\n", "[ ", testObj["name"], " ]" appUrls = testObj["urls"] layerUrls = testObj["layers"] # One way to iterate over an array: print "Looping technique A:" for url in appUrls: print " ", url # Another way to iterate over an array: # This technique is better since you get the array index # which you can use to read the corresponding element in # layerUrls. print "Looping technique B:" for index, url in enumerate(appUrls): print " ", index, url, layerUrls[index] # This technique works too: print "Looping technique C:" for index in range(len(appUrls)): print " ", index, appUrls[index], layerUrls[index] # This technique is even better: print "Looping technique D:" for appUrl, layerUrl in zip(appUrls, layerUrls): print " ", appUrl, layerUrl
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question