How can I get python to enumarate through an array instead of through a string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Nancy Eustice

    How can I get python to enumarate through an array instead of through a string

    I want to enumerate through an array called zones; within each zone I want to enumerate through another array zoneIDX{X} (X is the index of the zones array) When I make a new variable called snotelIDX and then try to enumerate through the array that the new variable is associated with it enumerates through the string instead of the array
    Code:
    zones=["Zone 1 - Yellowstone NP","Zone 2 - Absaroka Mtns","Zone 7 - Owl Creek/Bridger Mtns","Zone 8 - Bighorn Mtns West"]
    ##  Snotels in Zone 1 - Yellowstone NP
    zoneIDX0=["CANYON","LEWIS LAKE DIVIDE","SNAKE RIVER STATION","SYLVAN LAKE","SYLVAN ROAD","THUMB DIVIDE","TWO OCEAN PLATEAU"]
    ## Snotels in Zone 2 - Absaroka Mtns
    zoneIDX1=["BEARTOOTH LAKE","BLACKWATER","BURROUGHS CREEK","CASTLE CREEK","EVENING STAR","KIRWIN","MARQUETTE","PARKER PEAK","TIMBER CREEK","WOLVERINE","YOUNTS PEAK"]
    ## Snotels in Zone 7 - Owl Creek/Bridger Mtns
    zoneIDX2=["OWL CREEK"]
    ## Snotels in Zone 8 - Bighorn Mtns West
    zoneIDX3=["BALD MOUNTAIN","BONE SPRINGS DIVIDE","POWDER RIVER PASS","SHELL CREEK"]
    
    for j, v in enumerate(zones):
    		IDX = j
    		snotelIDX = "zoneIDX" + str(IDX)
    		for k, w in enumerate(snotelIDX):
    if I print snotelIDX it prints the right thing
    zoneIDX0
    zoneIDX1
    zoneIDX2 and so on
    but enumerates through s n o t e l I D X
    thanks
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Python is doing exactly what you told it to do. You are assigning a string to the variable snote1IDX. A string is a sequence similar to a list. Are you trying to assign each element in zones to a variable identifier? If so, this can be accomplished using globals().updat e() or exec().
    Code:
    for j, v in enumerate(zones):
        globals().update({"snote1%s" % j: v})
        for k, w in enumerate(v):
            print "snote1%s" % j, w
    
    # another way
    for j, v in enumerate(zones):
        exec "snote2%s=v" % (j)
    Note that v is a reference to the sublist in zones so that any modification to v will also modify the sublist in zones.

    Or did you mean to do this:
    Code:
    for j, v in enumerate(zones):
            IDX = j
            snotelIDX = "zoneIDX" + str(IDX)
            for k, w in enumerate(v):
    Last edited by bvdet; Sep 27 '10, 10:34 PM. Reason: Afterthought

    Comment

    • Nancy Eustice

      #3
      What I want is to enumerate through the zones and within each zone enumerate through the zoneIDX that goes with the zone out put like
      Zone 1 - Yellowstone NP
      CANYON
      LEWIS LAKE DIVIDE
      SNAKE RIVER STATION
      SYLVAN LAKE
      SYLVAN ROAD
      THUMB DIVIDE
      TWO OCEAN PLATEAU
      Zone 2 - Absaroka Mtns
      BEARTOOTH LAKE
      BLACKWATER
      BURROUGHS CREEK
      CASTLE CREEK
      EVENING STAR
      KIRWIN
      MARQUETTE
      PARKER PEAK
      TIMBER CREEK
      WOLVERINE
      YOUNTS PEAK
      Zone 7 - Owl Creek/Bridger Mtns
      OWL CREEK
      Zone 8 - Bighorn Mtns West
      BALD MOUNTAIN
      BONE SPRINGS DIVIDE
      POWDER RIVER PASS
      SHELL CREEK

      I tried the suggestions but it did not enumerate through them correctly
      Thanks for your help

      Comment

      • dwblas
        Recognized Expert Contributor
        • May 2008
        • 626

        #4
        You would use two lists that correspond to each other.
        Code:
        zones=["Zone 1 - Yellowstone NP","Zone 2 - Absaroka Mtns","Zone 7 - Owl Creek/Bridger Mtns","Zone 8 - Bighorn Mtns West"]
        ##  Snotels in Zone 1 - Yellowstone NP
        zoneIDX0=["CANYON","LEWIS LAKE DIVIDE","SNAKE RIVER STATION","SYLVAN LAKE","SYLVAN ROAD","THUMB DIVIDE","TWO OCEAN PLATEAU"]
        ## Snotels in Zone 2 - Absaroka Mtns
        zoneIDX1=["BEARTOOTH LAKE","BLACKWATER","BURROUGHS CREEK","CASTLE CREEK","EVENING STAR","KIRWIN","MARQUETTE","PARKER PEAK","TIMBER CREEK","WOLVERINE","YOUNTS PEAK"]
        ## Snotels in Zone 7 - Owl Creek/Bridger Mtns
        zoneIDX2=["OWL CREEK"]
        ## Snotels in Zone 8 - Bighorn Mtns West
        zoneIDX3=["BALD MOUNTAIN","BONE SPRINGS DIVIDE","POWDER RIVER PASS","SHELL CREEK"]
        zone_desc = [zoneIDX0, zoneIDX1, zoneIDX2, zoneIDX3]
        
        for ctr in range(len(zones)):
            print zones[ctr]
            for descr in zone_desc[ctr]:
                print "     ", descr

        Comment

        • Nancy Eustice

          #5
          dwblas - Thank you so much; Your code suggestion worked perfectly; just what I wanted.
          Thank you for taking the time to help me out!!!!

          Comment

          Working...