Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

2-D "for/while" statement

Status
Not open for further replies.

archeryguru2000

Programmer
Jun 19, 2007
1
0
0
US
Hello, I've been stumped for several weeks on this particlar program. At work here, we have 30 machines that we record scrap data on. I take this data and create spreadsheets (boring) that can be used for analysis. Anyway, I was wondering how I could change this "for" statement to a much more condensed amount of code.

for i in range(30):
while hold[n,3] == 1:
hold_01.append( hold[n,8] )
hDate_01.append( hold[n,0] )
n = n + 1
while hold[n,3] == 2:
hold_02.append( hold[n,8] )
hDate_02.append( hold[n,0] )
n = n + 1
while hold[n,3] == 3:
... and so on, for a total of 30 machines.

Is there anyway to alter the line(s) with the .append from a 1-D vector to a 2-D array, so I can set me while loop == i, and add the data from hold[] to the next row, and so on. Any help would be greatly appreciated.

Thanks,
~~archeryguru2000~~
 
Hello,

I am a newcomer to the Python language, so I could be completely wrong. I did find this example that looks like you can define a multi-dimensional array. I don't know if this example requires the Numeric Module or not but worth a try anyway.


For example (steps through a 30x10x100 array if the array already has data in it)

for i in range(30)
for j in range(10)
for k in range(100)
element = aData[j][k]
print element

Best of Luck.
Andy
 
Dear Archeryguru2000,

it's not completely clear what you are trying to achieve. Because what is the type of the 'hold' element. What is the information it's holding? I don't really follow the use of the comma.

That said, if you want to replace the long loop, you can replace the 30 repeats of the same type of code with a fairly simple solution:

Code:
locals()

Using this function you can dynamicly call your local values, e.g: hold_01, hold_02 etc...

so if you would use your old code it now would look like this:

Code:
while hold[n,3] <= 31:
        locals()['hold_%.2d' % n].append(hold[n,8])
        locals()['hDate_%.2d' % n].append(hold[n,0])
        n = n + 1

I'm not entirely sure under what condition you want it to stop but I just guessed, that the value coming from hold[n,3] is the same as the machine you're trying to write it to.

Yet again, I'm not sure what the square brackets are for by the 'hold' element. Please shine your light on that.

Bye Matey
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top