Field Calculate Sequential Numbers starting with specific 18 digit number

6589
29
Jump to solution
05-09-2016 07:41 AM
anayoung
New Contributor III

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

0 Kudos
29 Replies
RandyBurton
MVP Alum

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?

0 Kudos
DanPatterson_Retired
MVP Emeritus

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>

anayoung
New Contributor III

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.

0 Kudos
RandyBurton
MVP Alum

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)
JoshuaBixby
MVP Esteemed Contributor

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:

  • Strings in Python are sequences, which means you can iterate directly over them and don't need to convert them to a list.  That is, UC = "ABCD...XYZ" will work.
  • When working with very large sequences/sets/lists, sometimes it is advantageous to use generator expressions instead of list comprehensions since lists are fully built in memory while a generator expression is evaluated one element at a time.  That is, cr_vals = (c + r for c in UC for r in UC) could also work.
  • A while True: loop isn't common.  I used such a construct in an earlier post because I was looking for an infinite loop for my generator since I didn't want to rely on knowing the number of records ahead of time.  In your case, there is a maximum number of elements (3380) so you can structure your loop differently to avoid having to use a counter and modulus operator.

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
DanPatterson_Retired
MVP Emeritus

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. 

RandyBurton
MVP Alum

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'.

0 Kudos
DanPatterson_Retired
MVP Emeritus

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

0 Kudos
anayoung
New Contributor III

Dan and Randy...you guys are awesome! Love it!

0 Kudos
anayoung
New Contributor III

Way over my head, guys! But I'm enjoying the conversation. Thanks for all of the great stuff!