-1

USING IDLE/Python 3.5.1

May I first of all begin by saying I am a reasonably experienced programmer in VBA but am on day 2 of Python. I assure you I have conducted many searches on this question but the 30 or so documents I have read do not seem to explain my problem.

May I also please request that any answers given are properly formatted code for Python 3.5.1 rather than helpful pointers to other documentation or links?

The Problem

I am running a report and outputting results as I go. I need to store the results (presumably in an array) during this so that I can refer to them afterwards. The report (and the populating of the array) can be rerun multiple times so please bear that in mind if using concepts like 'append' when building the array. The array has dimensions [25,4] - a maximum of 25 records with four items in each.

Day   X  Y  Z  Total
1     2  3  4  9
2     3  4  5  12 ...

(Purists: The total needs to be recorded rather than calculated because of rounding.)

I could solve the problem myself if someone could translate this bit of code into Python (from VBA for illustration purposes). I do not want to import the arrays module unless it's the only way. Note: Variable l is a loop that makes the array get built twice to demonstrate that the array needs to be capable of rebuilding from scratch rather than being created just the once.

Sub sArray()
    Dim a(25, 4)
    For l = 1 To 2 
        For i = 1 To 25
            For j = 1 To 4
                a(i, j) = Int(100 * Rnd(1)) + 1
                Debug.Print a(i, j);
            Next j
        Next i
    Next l
End Sub

Thanks,

Tom

Tom PC
  • 11
  • 1
  • Generally, ordered sequences of objects are handled in python `list`s. `array`s have more specific, less common use cases. [Are you sure you want arrays?](http://stackoverflow.com/questions/176011/python-list-vs-array-when-to-use) – glibdud Sep 02 '16 at 12:21
  • I do not need to use arrays if lists are the norm in Python. I just need to know how to do the equivalent. – Tom PC Sep 02 '16 at 12:35

1 Answers1

0

I am not sure I got your question correctly... If you want to make an array (list i a better term in this case) of size [25,4] this is one way to go:

import random
a = [[int(100*random.random())+1 for j in range(4)] for i in range(25)]

>>> print a
[[74, 17, 36, 75],
 [1, 79, 33, 90],
 [58, 66, 47, 95],
 [35, 40, 87, 38],
 [43, 46, 34, 66],
 [69, 34, 26, 49],
 [56, 83, 44, 14],
 [2, 44, 54, 97],
 [50, 21, 39, 60],
 [13, 94, 12, 48],
 [36, 13, 2, 71],
 [77, 44, 31, 11],
 [56, 26, 30, 39],
 [17, 13, 83, 84],
 [54, 37, 34, 18],
 [5, 54, 88, 100],
 [22, 77, 70, 21],
 [51, 88, 26, 97],
 [69, 33, 86, 48],
 [42, 66, 38, 78],
 [71, 43, 96, 23],
 [6, 46, 100, 29],
 [32, 86, 15, 48],
 [96, 84, 8, 56],
 [29, 64, 69, 79]]

if you want to show that "the array needs to be capable of rebuilding from scratch rather than being created just the once" (why would you need this??)

for l in range(2):
    a = [[int(100*random.random())+1 for j in range(4)] for i in range(25)]

Also, the way of generating random numbers is odd (I have translated you method). To get the same result in python, just use random.randint(1,100) to generate random integers from 1 (i think you do not want to have 0 there) to whatever number you like.

If I have correctly understood from your comments, this is what you want:

def report(g=25):
    array = []
    for _ in range(g):
        x = random.randint(1,10)
        y = random.randint(1,10)
        z = random.randint(1,10)
        total = x+y+x
        row = [x,y,z,total]
        print(row)
        array.append(row)
    return array

result = report()
#prints all the rows while computing

>>> result #stores the "array"
[8, 4, 3, 20]
[10, 7, 4, 27]
[2, 4, 5, 8]
[8, 5, 8, 21]
[9, 7, 2, 25]
[2, 2, 3, 6]
[5, 8, 6, 18]
[8, 6, 1, 22]
[7, 6, 4, 20]
[7, 2, 10, 16]
[6, 5, 9, 17]
[3, 8, 8, 14]
[9, 1, 9, 19]
[1, 7, 7, 9]
[6, 6, 2, 18]
[9, 10, 1, 28]
[4, 6, 2, 14]
[6, 1, 6, 13]
[4, 1, 3, 9]
[5, 3, 5, 13]
[7, 5, 2, 19]
[9, 5, 7, 23]
[2, 5, 8, 9]
[3, 10, 4, 16]
[5, 6, 5, 16]
alec_djinn
  • 10,104
  • 8
  • 46
  • 71
  • You should probably note that while you're using the asker's terminology of "array", you're actually dealing with lists. [Arrays](https://docs.python.org/3/library/array.html) do exist in python and are a different animal. – glibdud Sep 02 '16 at 12:25
  • The guy expressed asked to avoid importing `array`, so `list` is the only option I think. – alec_djinn Sep 02 '16 at 12:27
  • I agree, but continuing to conflate the terms in your answer is just going to cause more confusion. – glibdud Sep 02 '16 at 12:29
  • OK. Back to basics. 1. I run a function that outputs a report of calculated data (not random numbers - they are just for illustration). 2. I need to store the results for later use. I presume an array or list would be the place to store them. 3. I may rerun my report - the 'array' needs to be overwritten. Does that help to clarify the situation? – Tom PC Sep 02 '16 at 12:39
  • Well I don't see any difference, you will call your function instad of random.random(), you will still have the results stored in a list of lists with "shape" 25,4. Isn't what you want? – alec_djinn Sep 02 '16 at 12:42
  • Hi alec. So far I have not been shown how to store my results in a list (except your instant list where the elements are pre-defined and predictable). The function 'Report' that I run uses a loop where variable G goes from 1 to 25. Four variables are calculated during the loop: X, Y, Z and Total. As they are calculated they are output to the screen. What I want to do is store them as we go in an array or list. – Tom PC Sep 02 '16 at 12:54
  • @TomPC You should edit your question then because it is not clear. Also, you may want to modify the function itself to return a list... if you post your function I can help you with that – alec_djinn Sep 02 '16 at 12:57
  • I disagree. I mention 'presumably in an array'. I mention I am on Day 2 of Python. I mention the problem I am trying to solve. With the greatest of respect I think you only read the VBA code and tried to replicate it in a simple way. – Tom PC Sep 02 '16 at 12:58
  • 1
    @TomPC You wrote "I could solve the problem myself if someone could translate this bit of code into Python" I did exactly that! – alec_djinn Sep 02 '16 at 13:01
  • Not with loops and within the context of the question. Thanks for your help, though. – Tom PC Sep 02 '16 at 13:19
  • @TomPC Does my last edit solve answer your question? – alec_djinn Sep 02 '16 at 13:37
  • Ah - that's looking more interesting... bear with me while I fiddle with it. Thanks. – Tom PC Sep 02 '16 at 14:26
  • Thank you very much, alec. I have used what you wrote successfully in my program – Tom PC Sep 02 '16 at 14:41
  • @TomPC Great! You can set the answer as solution now. – alec_djinn Sep 03 '16 at 15:08