Python Forum
How get attributes of maps from loop statement
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How get attributes of maps from loop statement
#2
This doesn't answer your question, but makes your code much more readable, and corrects
PEP8 inconsistencies.

You can save your dictionary as a json file with:
import json

landmask_regions= { \
        'Africa': ['mask_M01.map', 'mask_M02.map', 'mask_M03.map', \
            'mask_M05.map', 'mask_M06.map', 'mask_M07.map', 'mask_M08.map', \
            'mask_M09.map'], \
        'Asia': ['mask_M04.map', 'mask_M05.map', 'mask_M10.map', 'mask_M11.map', \
            'mask_M12.map', 'mask_M13.map', 'mask_M14.map', 'mask_M15.map', \
            'mask_M16.map', 'mask_M17.map', 'mask_M18.map', 'mask_M19.map', \
            'mask_M20.map', 'mask_M21.map', 'mask_M22.map', 'mask_M23.map'], \
        'Europe': ['mask_M26.map', 'mask_M28.map', 'mask_M32.map', 'mask_M33.map', \
            'mask_M34.map'], \
        'N-America': ['mask_M25.map', 'mask_M35.map', 'mask_M36.map', 'mask_M37.map', \
            'mask_M38.map', 'mask_M39.map', 'mask_M40.map', 'mask_M41.map', \
            'mask_M42.map', 'mask_M43.map', 'mask_M44.map', 'mask_M45.map', 'mask_M46.map'], \
        'Australia': ['mask_M47.map', 'mask_M48.map', 'mask_M49.map'], \
        'S-America': ['mask_M24.map', 'mask_M50.map', 'mask_M51.map', \
            'mask_M52.map', 'mask_M53.map']}

with open('landmarks.json', 'w') as jp:
    json.dump(landmask_regions, jp)
and modify your code like (cleaner):
import json


def main():
    with open('landmarks.json') as jp:
        landmask_regions = json.load(jp)

    choice = 'Europe'
    if choice == 'whole_world':
        maps = sum(landmask_regions.values(), [])
        print "processing world maps"
        for region in maps:
            print region
    elif choice in landmask_regions:
        maps = landmask_regions[choice]
        print "processing continent"
        for region in maps:
            print region
    elif choice.endswith('.map'):
        maps = [choice]
        print "processing river area"
        for region in maps:
            print region


if __name__ == '__main__':
    main()
Reply


Messages In This Thread
RE: How get attributes of maps from loop statement - by Larz60+ - Aug-21-2018, 02:18 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with nested maps Unkovic 10 1,490 Nov-01-2023, 03:45 PM
Last Post: Unkovic
Photo Python code: While loop with if statement HAMOUDA 1 628 Sep-18-2023, 11:18 AM
Last Post: deanhystad
  Multiply and Addition in the same loop statement with logic. joelraj 2 1,076 Feb-02-2023, 04:33 AM
Last Post: deanhystad
  Compare each element of an array in a logic statement without using a for loop leocsmith 3 5,952 Apr-01-2021, 07:57 PM
Last Post: deanhystad
  how to create pythonic codes including for loop and if statement? aupres 1 1,952 Jan-02-2021, 06:10 AM
Last Post: Gribouillis
  if statement in for loop researcher123 6 2,663 Oct-01-2020, 05:07 PM
Last Post: deanhystad
  Help: list comprehension for loop with double if statement mart79 3 2,482 May-04-2020, 06:34 AM
Last Post: buran
  Best way to map trajectory data on Google Maps Gduffley 1 2,705 Feb-05-2020, 12:36 AM
Last Post: scidam
  Can't visualize maps using Gmaps mPlummers 0 3,582 Sep-11-2019, 02:38 PM
Last Post: mPlummers
  Why doesn't my loop work correctly? (problem with a break statement) steckinreinhart619 2 3,252 Jun-11-2019, 10:02 AM
Last Post: steckinreinhart619

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020