So I have a program I am making but I need to make it more efficient, is there any way to condernse the following code into a more compact and efficient code?

r1p1=o
    r1p2=o
    r1p3=o
    r1p4=o
    r2p1=o
    r2p2=o
    r2p3=o
    r2p4=o
    r3p1=o
    r3p2=o
    r3p3=o
    r3p4=o
    r4p1=o
    r4p2=o
    r4p3=o
    r4p4=o
    r5p1=o
    r5p2=o
    r5p3=o
    r5p4=o
    r6p1=o
    r6p2=o
    r6p3=o
    r6p4=o
    r7p1=o
    r7p2=o
    r7p3=o
    r7p4=o
    r8p1=o
    r8p2=o
    r8p3=o
    r8p4=o
    r9p1=o
    r9p2=o
    r9p3=o
    r9p4=o
    r10p1=o
    r10p2=o
    r10p3=o
    r10p4=o
    r11p1=o
    r11p2=o
    r11p3=o
    r11p4=o
    r12p1=o
    r12p2=o
    r12p3=o
    r12p4=o
    r13p1=o
    r13p2=o
    r13p3=o
    r13p4=o
    r14p1=o
    r14p2=o
    r14p3=o
    r14p4=o
    r15p1=o
    r15p2=o
    r15p3=o
    r15p4=o
    r16p1=o
    r16p2=o
    r16p3=o
    r16p4=o
    r17p1=o
    r17p2=o
    r17p3=o
    r17p4=o
    r18p1=o
    r18p2=o
    r18p3=o
    r18p4=o
    r19p1=o
    r19p2=o
    r19p3=o
    r19p4=o
    r20p1=o
    r20p2=o
    r20p3=o
    r20p4=o

why you don't use list or tuple?

If you can tolerate to use for instance rp[(7, 3)] instead of r7p3, then a dictionary can well represent the 2d array you are talking about.

# create a dictionary with (r, p) key tuples
# where r is the number after r and p is the number after p
# so r7p3 in your case would be (7, 3)
# initializes all values to zero or whatever
rp = dict(((r, p), 0) for r in range(1, 21) for p in range(1, 5))

# show the value of 
r = 9
p = 2
print(rp[(r, p)])

# change the value of
r = 5
p = 3
rp[(r, p)] = 77
# show it
print(rp[(r, p)])

print('-'*15)  # cosmetic line

# change a whole range of values
for r in range(2, 4):
    for p in range(1, 5):
        rp[(r, p)] = 8

# check the changes
for r in range(2, 4):
    for p in range(1, 5):
        print("rp[(%d, %d)] = %s" % (r, p, rp[(r, p)]))

'''result ..
0
77
---------------
rp[(2, 1)] = 8
rp[(2, 2)] = 8
rp[(2, 3)] = 8
rp[(2, 4)] = 8
rp[(3, 1)] = 8
rp[(3, 2)] = 8
rp[(3, 3)] = 8
rp[(3, 4)] = 8
'''

Note that the variables in the original post are set to the value of lowercase "o" but the principle is the same. Another example of why not to use it as a single letter name. You could also use a list of 81 elements, ignoring the [0] element, although the dictionary is more straight forward.

element = 69
r,p = divmod(element, 4)
print "r=%d, p=%d" % (r, p)

print "element =", r*4+p
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.