0

I have a list of "string indexes" built following this pattern:

allIndexes = ['A', 'B', 'C'... 'Z', 'AA', 'AB',... 'AZ'... 'ZZ', 'AAA', 'AAB'...]

Now, let's say I want to create a new unique index. Here is my code so far:

import string

indexes = ['A', 'AA', 'CB', 'B', 'H', 'IZ']

x = 0
for i in indexes:

    if string.ascii_uppercase[x] == i:
        x += 1

    else:
        newIndex = string.ascii_uppercase[x]

print newIndex
>>> C

It works well for the entries from 'A' to 'Z', but how can I make it work for any index following the pattern?

I mean, given an unordered list of "string indexes", how do I find the lowest string index that doesn't appear in that list?
In the code sample, the list contains "A" and "B", so the next lowest string index is "C". But the code fails if the list doesn't contains all string indexes of length 1. Suppose indexes was ["A", "B", ... "Z", "AA"]. Then newIndex should be "AB".

UKDP
  • 226
  • 5
  • 21
  • Terminology nitpick: there are no dictionaries in this question. – Kevin May 25 '16 at 13:56
  • 1
    I don't quite understand what you are trying to do, what should the output of your code be and what are you seeing? – Zafi May 25 '16 at 14:02
  • 1
    I'm interpreting this question as "given an unordered list of 'string indexes', how do I find the lowest string index that doesn't appear in that list?". In the code sample, the list contains "A" and "B", so the next lowest string index is "C". OP's code apparently fails if the list contains all string indexes of length 1. Suppose `indexes` was `["A", "B", ... "Z", "AA"]`. Then `newindex` should be "AB". – Kevin May 25 '16 at 14:08
  • @Kevin That is exactly what I mean. I'll re-phrase my post this way if you agree. – UKDP May 25 '16 at 14:22
  • 1
    This has been asked many times with minor variations. Search for "ZZ AAA" and you'll get lots of answers how to translate between Excel column names and their indices (and vice versa). – Tim Pietzcker May 25 '16 at 14:22

1 Answers1

1
import string
from itertools import product

indexes = set(['A', 'AA', 'CB', 'B', 'H', 'IZ'])
all_indexes = set([''])
unused_indexes = set()

while (unused_indexes == set()):
    all_indexes = set([''.join(p) for p in product(all_indexes, list(string.ascii_uppercase))])
    unused_indexes = all_indexes - indexes
print(unused_indexes) # this prints out all indexes that are available at that length
print(sorted(list(unused_indexes))[0]) # this prints out the first available index
>>> C
Jon McClung
  • 1,619
  • 20
  • 28