GreyMamba

Thinking Allowed … (under construction)

Thinking Allowed … (under construction)

Nothing is interesting if you're not interested
Helen Clark MacInnes
Python Stuff

I tend to spend too much time mucking around with Python code - as you can see from the bits of code scattered around this site. So, I've decided to add a page specifically for stuff related to just Python. It'll include snippets of code, interesting applications, algorithms or anything else Python related. Code that is purely there as a means to demonstrate or calculate something specific will remain as it is, in the most relevant post. Most of this stuff will be migrating to my sister pages -

- in the near future.

Sorry if you were hoping to see something on the Pythonidae family of snakes - interesting as they are.

Incremental row,column indices

Not sure if this is particularly efficient, but I used the Python modulus operator, '%'. The following code gives a function that produces an iterable object that produces successive row,col pairs. It also shows how to use a couple of lines of code within something - looping through a list, say.

# Creates a series of incremental row and column indices
# Returns and iterable containg tuple of i,j indices

def row_col_idx(r,c):
    it = []
    for k in range(r*c):
        j = (k % c + 1)
        i = (int(k/c))%max(r,c) + 1
        it.append ((i,j))
        #print("r =",i,"c =",j)
    return iter(it)

# And here's how it works

fred = row_col_idx(10,4)
for i,j in fred:
    print (str(i)+','+str(j))

# Or, more directly:

r=3
c=2
my_list = ['one', 'two', 'three', 'four', 'five', 'six']
for k, numb in enumerate(my_list):
    j = (k % c)
    i = (int(k/c))%max(r,c)
    print (str(i)+','+str(j)+'th numberis: '+numb)
Back
RapidWeaver Icon

Made in RapidWeaver