473,386 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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
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
4 1832
bvdet
2,851 Expert Mod 2GB
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
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
dwblas
626 Expert 512MB
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
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

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

Similar topics

5
by: Steve G | last post by:
Steve G Feb 1, 1:12 pm show options From: "Steve G" <sgr...@computicle.com> - Find messages by this author Date: Tue, 01 Feb 2005 13:12:42 -0800 Local: Tues, Feb 1 2005 1:12 pm Subject:...
3
by: Alexander Muylaert | last post by:
Hi In delphi we can create an array of an enum. type TMyEnum = (meRed, meGreen, meBlue); const cMyArray : array of TColor = (clRed, clGreen, clBlue);
4
by: ALI-R | last post by:
Is it a possibel to assign an array to a DataColumn then bind it to a Combo box?? Is the follwoing code right? public static DataSet InsertParamsToDS(ReportParameter arrParams) { DataSet...
1
by: Hrvoje Voda | last post by:
How to convert an array byte into string? Hrcko
3
by: Andrew | last post by:
Hi, How do I convert an array to a string ? What I did was convert a string to an array, after some manipulation, I want to pass the value back to a string. char setchecked; setchecked =...
3
by: Sakharam Phapale | last post by:
Hi All, How to get the no of memory bytes allocated to the Array of type String? Dim arrDetail(100,10) As String Thanks and Regards Sakharam Phapale
2
by: Jaime Stuardo | last post by:
Hi all... I'm trying to retrieve a SQLXML query using VB.NET. When I programmed in VB 6.0, I used Stream object to accomplish this which was trivial. I cannot do the same thing in VB.NET. Here...
7
by: Allerdyce.John | last post by:
Hi, I am new to python. I would like to know how to use python regular expression to substitute string value? I have an input string like this: x:11 y:0 w:760 h:19 area:14440 areaPerCent:0...
1
by: koval | last post by:
Type mismatch error occured when calling HTML array elements through Request.form("txtlrno" & i) after self posting the form, please anyone help me immediately because is in crisis.
0
by: dudeja.rajat | last post by:
On Tue, Aug 19, 2008 at 12:20 PM, Fredrik Lundh <fredrik@pythonware.comwrote: Fredrik, My apology for any confusion created. I read all the replies. In fact I'm thankful to you all guys who are...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.