473,414 Members | 1,563 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,414 software developers and data experts.

2-D "for/while" loop

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.
Expand|Select|Wrap|Line Numbers
  1. for i in range(30):
  2.      while hold[n,3] == 1:
  3.           hold_01.append( hold[n,8] )
  4.           hDate_01.append( hold[n,0] )
  5.           n = n + 1
  6.      while hold[n,3] == 2:
  7.           hold_02.append( hold[n,8] )
  8.           hDate_02.append( hold[n,0] )
  9.           n = n + 1
  10.      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~~
Jun 19 '07 #1
3 2121
bvdet
2,851 Expert Mod 2GB
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~~
Please use code tags around your code. Can you show us the structure of your original data and the output you are trying to achieve?
Maybe you are looking for something like this:
Expand|Select|Wrap|Line Numbers
  1. dataDict = dict.fromkeys(['hold_%02d' % i for i in range(1,31)], [])
  2. keys = dataDict.keys()
  3. keys.sort()
  4. for i, key in enumerate(keys):
  5.     dataDict[key] = [hold[i,8], hold[i,0]]
From the dictionary you can write a 2D CSV file for your spreadsheet.
Jun 19 '07 #2
bartonc
6,596 Expert 4TB
Please use code tags around your code. Can you show us the structure of your original data and the output you are trying to achieve?
Maybe you are looking for something like this:
Expand|Select|Wrap|Line Numbers
  1. dataDict = dict.fromkeys(['hold_%02d' % i for i in range(1,31)], [])
  2. keys = dataDict.keys()
  3. keys.sort()
  4. for i, key in enumerate(keys):
  5.     dataDict[key] = [hold[i,8], hold[i,0]]
From the dictionary you can write a 2D CSV file for your spreadsheet.
I alway like your dictionary solutions. I was going to say: put lists into a list:
Expand|Select|Wrap|Line Numbers
  1. hold_01 = ['a', 'list', 'of', 'stuff']
  2. hold_02 = ['another', 'list', 'of', 'stuff']
  3.  
  4. holdList = [eval('hold_%02d' %i) for i in range(1, 3)]
  5. print holdList
  6.  
Then in the while loop it's:
Expand|Select|Wrap|Line Numbers
  1. holdList[i].append( hold[n,8] )
Jun 19 '07 #3
bartonc
6,596 Expert 4TB
I alway like your dictionary solutions. I was going to say: put lists into a list:
Expand|Select|Wrap|Line Numbers
  1. hold_01 = ['a', 'list', 'of', 'stuff']
  2. hold_02 = ['another', 'list', 'of', 'stuff']
  3.  
  4. holdList = [eval('hold_%02d' %i) for i in range(1, 3)]
  5. print holdList
  6.  
Then in the while loop it's:
Expand|Select|Wrap|Line Numbers
  1. holdList[i].append( hold[n,8] )
Of course, if all you need is a 2D empty list, the syntax would be:
Expand|Select|Wrap|Line Numbers
  1. holdList = [[] for i in range(30)]
Jun 19 '07 #4

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

Similar topics

6
by: Sybren Stuvel | last post by:
Hi there, Is it possible to use an assignment in a while-loop? I'd like to do something like "loop while there is still something to be read, and if there is, put it in this variable". I've been...
23
by: Invalid User | last post by:
While trying to print a none empty list, I accidentaly put an "else" statement with a "for" instead of "if". Here is what I had: if ( len(mylist)> 0) : for x,y in mylist: print x,y else:...
32
by: Toby Newman | last post by:
At the page: http://www.strath.ac.uk/IT/Docs/Ccourse/subsection3_8_3.html#SECTION0008300000000000000 or http://tinyurl.com/4ptzs the author warns: "The for loop is frequently used, usually...
6
by: John Pass | last post by:
What is the difference between a While and Do While/Loop repetition structure. If they is no difference (as it seems) why do both exist?
5
by: Fabian Vilers | last post by:
Hi again... I'm wondering what could be better in terms of performance between: var my_array = new Array(); // populate array for (index in my_array) { // do something with my_array
9
by: morpheus | last post by:
Hi Group, When I run this: # include <stdio.h> int main(){ int c=0; while ( (c=getchar()) != EOF && c != ' ' && c != '\t' ) printf("foo"); if (c == '8') putchar(c);
34
by: Frederick Gotham | last post by:
Is the domestic usage of the C "for" loop inefficient when it comes to simple incrementation? Here's a very simple program that prints out the bit-numbers in a byte. #include <stdio.h> #include...
1
by: Wazza | last post by:
G'Day, I have a re-occurring problem and wish to design a framework or pattern to address it. I have some Ideas but I would like to do some brainstorming with my peers before commencing. The...
1
by: Kerem Gümrükcü | last post by:
Hi All, i am too tired now to find this out by my own, but why does my while loop "while(readline==Console.ReadLine())" needs twice return hit to read the line, after setting...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.