-2

I am trying to adhere to PEP8 guidelines and one that I am having trouble with is the 79 character line limit. If I am working something like a 10x10 array I tend to use nested loops to access specific elements of the array. It looks like this:

size=10
for i in range(size):
    for k in range(size):
        for j in range(size):
            for l in range(size):
                for m in range(size):
                    for n in range(size):
                        for p in range(size):
                            for q in range(size):
                                for r in range(size):
                                    for s in range(size):
                                        matrix[i][k][j][l][m][n][p][q][r][s]=matrix[i][k][j][l][m][n][p][q][r][s]*matrix[i][k][j][l][m][n][p][q][r][s]

How should I alter this to fit the character line limit?

I rewrote the code recursively and renamed some variables, now it fits! Thank you.

size=10
def sq(element,power):
    if power==1:
        return element
    else:
        return element*sq(element,power-1)

for i in range(size):
    for j in range(size):
        for k in range(size):
            for l in range(size):
                for m in range(size):
                    for n in range(size):
                        for p in range(size):
                            for q in range(size):
                                for r in range(size):
                                    for s in range(size):
                                       x=l[i][j][k][l][m][n][p][q][r][s]
                                       l[i][j][k][l][m][n][p][q][r][s]=sq(x,2)
  • 3
    You should rewrite it to **not do that** - the problem isn't the styling, it's the approach. Look into `itertools` or recursion. – jonrsharpe May 13 '16 at 16:45
  • I assume the order `i, k, j` is just there to confuse everone? – Karoly Horvath May 13 '16 at 16:56
  • 2
    I very much doubt you have a matrix object with ten billion elements in it.. – DSM May 13 '16 at 17:53
  • "I rewrote the code recursively" well no you still have 10 nested loops, and if you want to write good code renaming `matrix` to `l` is not a very good solution... Especially since `l` is one of your **indices** so your 'new' code just raises a `TypeError`. – Tadhg McDonald-Jensen May 13 '16 at 18:54
  • 1
    @TadhgMcDonald-Jensen but he did rewrite his squaring code to be recursive, just to slow down this program even more! After that change, I'm thinking SO troll and we all played into it. Seriously, only 0.06% of SO Python questions are about PEP8 issues, what are the odds? – cdlane May 13 '16 at 21:47
  • The real solution is to use numpy. – Andy Hayden May 25 '16 at 04:29

2 Answers2

2

Like what cdlane did, but also don't handle the indices manually.

from itertools import product, cycle
from pprint import pprint

def tuple_index(arr, index):
    """
    tuple_index(arr, [i, j, ..., k]) == arr[i][j]...[k]
    """
    result = arr
    for i in index:
        result = result[i]
    return result

def main():
    size = 4
    dimensions = 3

    # Build up sample matrix manually to test and demonstrate
    source = cycle(range(20))
    matrix = [[[next(source)
                for i in range(size)]
               for j in range(size)]
              for k in range(size)]
    print('Before:')
    pprint(matrix)

    for index in product(range(size), repeat=dimensions):
        inner_list = tuple_index(matrix, index[:-1])
        inner_list[index[-1]] *= inner_list[index[-1]]

    print('\nAfter:')
    pprint(matrix)

main()

Output:

Before:
[[[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11], [12, 13, 14, 15]],
 [[16, 17, 18, 19], [0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]],
 [[12, 13, 14, 15], [16, 17, 18, 19], [0, 1, 2, 3], [4, 5, 6, 7]],
 [[8, 9, 10, 11], [12, 13, 14, 15], [16, 17, 18, 19], [0, 1, 2, 3]]]

After:
[[[0, 1, 4, 9], [16, 25, 36, 49], [64, 81, 100, 121], [144, 169, 196, 225]],
 [[256, 289, 324, 361], [0, 1, 4, 9], [16, 25, 36, 49], [64, 81, 100, 121]],
 [[144, 169, 196, 225], [256, 289, 324, 361], [0, 1, 4, 9], [16, 25, 36, 49]],
 [[64, 81, 100, 121],
  [144, 169, 196, 225],
  [256, 289, 324, 361],
  [0, 1, 4, 9]]]
Alex Hall
  • 34,833
  • 5
  • 57
  • 89
1

Although numpy probably handles this more elegantly & efficiently, you could try something like:

from itertools import product

for indicies in product(range(size), repeat=size):
    (i, j, k, l, m, n, p, q, r, s) = indicies
    matrix[i][j][k][l][m][n][p][q][r][s] **= 2
cdlane
  • 40,441
  • 5
  • 32
  • 81
  • you are missing `o`? And why not do `**=2` instead of `*= self`? – Tadhg McDonald-Jensen May 13 '16 at 18:48
  • @TadhgMcDonald-Jensen, not missing `o`, just matching OP's original variables. The `**=` is a nice touch, to keep under PEP8 line limit -- thank you! Though the answer was about using index permutations as a substitute for nested loops. Still waiting for 10 dimensional test code to actually return a result, however ... – cdlane May 13 '16 at 18:55
  • lol, yeah just `matrix = [1]*(10**10)` takes a really long time, then traversing it with 9 `__getitem__` calls and one `__setitem__` call each one.... Don't expect it to finish any time soon. – Tadhg McDonald-Jensen May 13 '16 at 19:03