2

I need to populate a two dimensional array with salary grades and steps.

salaryGrades = []
salaryStep = 0
salaryInc = 1000

for x in range (0,10):
    salaryStep+=100000
    salaryGrades.append([salaryStep])
    for x in salaryGrades:
        salaryInc=+1000
        x.append([salaryInc])


print (salaryGrades)

This obviously gives me an incorrect array. I need each array to increment by 10k and each nested array to increment by 1000.

i.e. [ [10000],[11000],[12000],...][20000],[21000][22000],...] ] up to 100k

How do I get this code to work, or is there a better way?

1
  • What is your question? Commented Mar 5, 2013 at 19:45

3 Answers 3

3

I'd use a list comprehension:

>>> salaryGrades = [list(range(s, s + 10000, 1000)) for s in range(10000, 100000, 10000)]
>>> salaryGrades
[[10000, 11000, 12000, 13000, 14000, 15000, 16000, 17000, 18000, 19000],
 [20000, 21000, 22000, 23000, 24000, 25000, 26000, 27000, 28000, 29000],
 [30000, 31000, 32000, 33000, 34000, 35000, 36000, 37000, 38000, 39000],
 [40000, 41000, 42000, 43000, 44000, 45000, 46000, 47000, 48000, 49000],
 [50000, 51000, 52000, 53000, 54000, 55000, 56000, 57000, 58000, 59000],
 [60000, 61000, 62000, 63000, 64000, 65000, 66000, 67000, 68000, 69000],
 [70000, 71000, 72000, 73000, 74000, 75000, 76000, 77000, 78000, 79000],
 [80000, 81000, 82000, 83000, 84000, 85000, 86000, 87000, 88000, 89000],
 [90000, 91000, 92000, 93000, 94000, 95000, 96000, 97000, 98000, 99000]]
Sign up to request clarification or add additional context in comments.

2 Comments

Why yes, yes I do. Granted, this syntax is a bit too short hand for me to comfortably wrap my head around. Anyway to translate it into syntax similar to OP?
That'll do. You are appreciated.
2

You could also do this as a numpy-onliner.

import numpy as np

print 1000*np.arange(10,100).reshape((9,10))


[[10000, 11000, 12000, 13000, 14000, 15000, 16000, 17000, 18000, 19000],
 [20000, 21000, 22000, 23000, 24000, 25000, 26000, 27000, 28000, 29000],
 [30000, 31000, 32000, 33000, 34000, 35000, 36000, 37000, 38000, 39000],
 [40000, 41000, 42000, 43000, 44000, 45000, 46000, 47000, 48000, 49000],
 [50000, 51000, 52000, 53000, 54000, 55000, 56000, 57000, 58000, 59000],
 [60000, 61000, 62000, 63000, 64000, 65000, 66000, 67000, 68000, 69000],
 [70000, 71000, 72000, 73000, 74000, 75000, 76000, 77000, 78000, 79000],
 [80000, 81000, 82000, 83000, 84000, 85000, 86000, 87000, 88000, 89000],
 [90000, 91000, 92000, 93000, 94000, 95000, 96000, 97000, 98000, 99000]]

Especially for large arrays this could speed your code, as the looping is then performed by the numpy module, which uses C.

Comments

0

Modify your code like so:

for y in range (0,8):
  for x in range (0,9):
    salaryGrades[y][x] = (y+1)*10000 + x*1000

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.