2-D "for/while" loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • archeryguru2000
    New Member
    • Jun 2007
    • 1

    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.
    [CODE=python]
    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:[/CODE]
    ... 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,
    ~~archeryguru20 00~~
    Last edited by bartonc; Jun 19 '07, 03:15 PM. Reason: Added [CODE=python][CODE] tags.
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by archeryguru2000
    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,
    ~~archeryguru20 00~~
    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:[code=Python]dataDict = dict.fromkeys(['hold_%02d' % i for i in range(1,31)], [])
    keys = dataDict.keys()
    keys.sort()
    for i, key in enumerate(keys) :
    dataDict[key] = [hold[i,8], hold[i,0]][/code]From the dictionary you can write a 2D CSV file for your spreadsheet.

    Comment

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #3
      Originally posted by bvdet
      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:[code=Python]dataDict = dict.fromkeys(['hold_%02d' % i for i in range(1,31)], [])
      keys = dataDict.keys()
      keys.sort()
      for i, key in enumerate(keys) :
      dataDict[key] = [hold[i,8], hold[i,0]][/code]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:[CODE=python]hold_01 = ['a', 'list', 'of', 'stuff']
      hold_02 = ['another', 'list', 'of', 'stuff']

      holdList = [eval('hold_%02d ' %i) for i in range(1, 3)]
      print holdList
      [/CODE]Then in the while loop it's:[CODE=python]holdList[i].append( hold[n,8] )[/CODE]

      Comment

      • bartonc
        Recognized Expert Expert
        • Sep 2006
        • 6478

        #4
        Originally posted by bartonc
        I alway like your dictionary solutions. I was going to say: put lists into a list:[CODE=python]hold_01 = ['a', 'list', 'of', 'stuff']
        hold_02 = ['another', 'list', 'of', 'stuff']

        holdList = [eval('hold_%02d ' %i) for i in range(1, 3)]
        print holdList
        [/CODE]Then in the while loop it's:[CODE=python]holdList[i].append( hold[n,8] )[/CODE]
        Of course, if all you need is a 2D empty list, the syntax would be:[CODE=python]holdList = [[] for i in range(30)][/CODE]

        Comment

        Working...