Hello,
I am need to field calculate sequential numbers starting with 033900000100191511. So, the numbering would be 033900000100191511, 033900000100191512, 033900000100191513 etc...
Can anyone help me with a python script for this?
With much appreciation,
Ana
Solved! Go to Solution.
I like Dan's code:
UC = list("ABCDEFGHIJKLMNOPQRSTUVWXYZ") cr_vals = [c + r for c in UC for r in UC] for j in range(5,10) : for i in range(0,676) : print str(j) + cr_vals
But can it be put in a generator?
try ( ) instead of [ ], I blogged about it in my LC series in any event have a look see
>>> a = [ i for i in range(10)]
>>> b = ( i for i in range(10))
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> b
<generator object <genexpr> at 0x106393090>
Hi Randy,
This would be very helpful if I knew much about generators. However, I would love to see how this code works inside of a generator. I've been looking but cannot find a simple how to set up a generator for this code. Do you know where I can find instructions? Not just broad instructions - I need step by step instructions.
I am also new to generators, but here's my code. You might need to trap errors if you go past the limit. (Thanks to Joshua and Dan for the introduction to generators.)
def gen(x): UC = list("ABCDEFGHIJKLMNOPQRSTUVWXYZ") cr_vals = [c + r for c in UC for r in UC] while True: yield str(x / 676 + 5) + cr_vals[x % 676] x+=1 g = gen(0) # print all combinations for loop in range(0, 3380): print next(g)
Randy, I applaud your efforts to broaden your Python knowledge and experiment with generators. Generators are one of the doors to higher-level Python functionality, so understanding and using them is worth the effort.
A couple of comments since you are learning:
def gen(start, stop, step=1): UC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" for n in xrange(start, stop, step): for i in (str(n) + c + r for c in UC for r in UC): yield i g = gen(5, 10) for j in g: print j
Randy... my bad on the list("AB...") thing.. I was translating a piece from a numpy snippet involved in a 'zip' which requires conversion to list in python 3.4... Joshua is right for straight python, just drop zip.
Thanks Joshua for the tips and the code example. It has been most helpful in learning about generators. I hope this has also helped Ana in her project.
About the use of the modulus operator, I thought it would be easier to keep track of one number instead of two for seeding the generator for Ana's needs. If she started at 0 and did 100 records, then she would use 100 for the seed the next time. When she reaches 3380, the generator would need to be modified to produce a new sequence of codes, such as 'ZAA'.
you can extend the range by using more combinations if needed, beyond that, it is time for a change in part of the classification scheme
>>> import string
>>> ul = string.uppercase + string.lowercase
>>> cr_vals = [c + r for c in ul for r in ul]
>>> len(cr_vals)
2704
Dan and Randy...you guys are awesome! Love it!