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 -
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
| |
Share this Question
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(). - 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: - for j, v in enumerate(zones):
-
IDX = j
-
snotelIDX = "zoneIDX" + str(IDX)
-
for k, w in enumerate(v):
| |
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
| | Expert 100+
P: 621
|
You would use two lists that correspond to each other. - 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
| |
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!!!!
| | | | Question stats - viewed: 1608
- replies: 4
- date asked: Sep 27 '10
|