By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,626 Members | 1,146 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,626 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Expand|Select|Wrap|Line Numbers
  1. zones=["Zone 1 - Yellowstone NP","Zone 2 - Absaroka Mtns","Zone 7 - Owl Creek/Bridger Mtns","Zone 8 - Bighorn Mtns West"]
  2. ##  Snotels in Zone 1 - Yellowstone NP
  3. zoneIDX0=["CANYON","LEWIS LAKE DIVIDE","SNAKE RIVER STATION","SYLVAN LAKE","SYLVAN ROAD","THUMB DIVIDE","TWO OCEAN PLATEAU"]
  4. ## Snotels in Zone 2 - Absaroka Mtns
  5. zoneIDX1=["BEARTOOTH LAKE","BLACKWATER","BURROUGHS CREEK","CASTLE CREEK","EVENING STAR","KIRWIN","MARQUETTE","PARKER PEAK","TIMBER CREEK","WOLVERINE","YOUNTS PEAK"]
  6. ## Snotels in Zone 7 - Owl Creek/Bridger Mtns
  7. zoneIDX2=["OWL CREEK"]
  8. ## Snotels in Zone 8 - Bighorn Mtns West
  9. zoneIDX3=["BALD MOUNTAIN","BONE SPRINGS DIVIDE","POWDER RIVER PASS","SHELL CREEK"]
  10.  
  11. for j, v in enumerate(zones):
  12.         IDX = j
  13.         snotelIDX = "zoneIDX" + str(IDX)
  14.         for k, w in enumerate(snotelIDX):
  15.  
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
Sep 27 '10 #1
Share this Question
Share on Google+
4 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
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().update() or exec().
Expand|Select|Wrap|Line Numbers
  1. for j, v in enumerate(zones):
  2.     globals().update({"snote1%s" % j: v})
  3.     for k, w in enumerate(v):
  4.         print "snote1%s" % j, w
  5.  
  6. # another way
  7. for j, v in enumerate(zones):
  8.     exec "snote2%s=v" % (j)
  9.  
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:
Expand|Select|Wrap|Line Numbers
  1. for j, v in enumerate(zones):
  2.         IDX = j
  3.         snotelIDX = "zoneIDX" + str(IDX)
  4.         for k, w in enumerate(v):
Sep 27 '10 #2

P: n/a
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
Sep 30 '10 #3

Expert 100+
P: 621
You would use two lists that correspond to each other.
Expand|Select|Wrap|Line Numbers
  1. zones=["Zone 1 - Yellowstone NP","Zone 2 - Absaroka Mtns","Zone 7 - Owl Creek/Bridger Mtns","Zone 8 - Bighorn Mtns West"]
  2. ##  Snotels in Zone 1 - Yellowstone NP
  3. zoneIDX0=["CANYON","LEWIS LAKE DIVIDE","SNAKE RIVER STATION","SYLVAN LAKE","SYLVAN ROAD","THUMB DIVIDE","TWO OCEAN PLATEAU"]
  4. ## Snotels in Zone 2 - Absaroka Mtns
  5. zoneIDX1=["BEARTOOTH LAKE","BLACKWATER","BURROUGHS CREEK","CASTLE CREEK","EVENING STAR","KIRWIN","MARQUETTE","PARKER PEAK","TIMBER CREEK","WOLVERINE","YOUNTS PEAK"]
  6. ## Snotels in Zone 7 - Owl Creek/Bridger Mtns
  7. zoneIDX2=["OWL CREEK"]
  8. ## Snotels in Zone 8 - Bighorn Mtns West
  9. zoneIDX3=["BALD MOUNTAIN","BONE SPRINGS DIVIDE","POWDER RIVER PASS","SHELL CREEK"]
  10. zone_desc = [zoneIDX0, zoneIDX1, zoneIDX2, zoneIDX3]
  11.  
  12. for ctr in range(len(zones)):
  13.     print zones[ctr]
  14.     for descr in zone_desc[ctr]:
  15.         print "     ", descr 
Sep 30 '10 #4

P: n/a
dwblas - Thank you so much; Your code suggestion worked perfectly; just what I wanted.
Thank you for taking the time to help me out!!!!
Oct 1 '10 #5

Post your reply

Sign in to post your reply or Sign up for a free account.