konrad
konrad

Reputation: 3706

iterating over list and concatenating alphabetical sequence in Python

I am trying to iterate over a list and assign a Alphabetical letter to each element within the list, and if there is a duplicate then assign next letter in alphabet to it as to get unique items.

sequence = [0, 1, 2, 3, 1, 4, 2, 1]
unique_seq = [0A, 1A, 2A, 3A, 1B, 4A, 2B, 1C]

I tried generating a list of alphabet letters like this:

alpha = list(map(chr, range(65, 91)))

Then i wanted to iterate over the sequence like this:

for i in sequence:
        unique_seq.append(i) for i in sequence if i not in unique_seq else...

I am not sure how to go about the rest of it...

Thank you,

Upvotes: 1

Views: 238

Answers (3)

Joe
Joe

Reputation: 2437

I made this as a generator. I'm using the Counter data structure from collections. This returns 0 if the key doesn't yet exist, instead of a key error. So we can then use this as the index for the alpha list.

Added a function to make the excel column style letters. Tested up to 1 million versions of the same number.

import collections

# Uncomment for Python 2.X
#from __future__ import division, print_function

sequence = [0, 1, 2, 3, 1, 4, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]

def get_excel_column(number):
    dividend = number
    column_name = []
    while dividend > 0:
        modulo = (dividend - 1) % 26
        column_name.insert(0, chr(modulo + 65))
        dividend = (dividend - modulo) // 26
    return ''.join(column_name)

def get_unique(seq):
    cnt = collections.Counter()
    for item in seq:
        cnt[item] += 1
        yield '%s%s' % ( str(item), get_excel_column(cnt[item]) )

for uniq in get_unique(sequence):
    print(uniq)

Output:

0A
1A
2A
3A
1B
...
1AC
1AD
1AE

Note: Python 3 syntax. Change print and division as needed.

Upvotes: 0

aruisdante
aruisdante

Reputation: 9075

Here is a solution that will work with a sequence of infinite size and with infinite number of repeats (memory allowing)

def increment_item(item = 'A'):
    '''
    Given a character sequence item, produces the next item in the character sequence set

    :type item: str
    :param item: The character sequence item to increment
    :rtype: str
    :return: The next element in the sequence. EX: item='A', return ='B'. item='Z', return ='AA'

    '''
    next_char = [ord(char) for char in item]
    next_char[-1] += 1
    for index in xrange(len(next_char)-1, -1, -1):
            if next_char[index] > ord('Z'):
                    next_char[index] = ord('A')
                    if index > 0:
                            next_char[index-1] += 1
                    else:
                            next_char.append(ord('A'))
    return ''.join((chr(char) for char in next_char))

def char_generator(start = 'A'):
    '''
    A generator which yields the next item in the character sequence every time next() is called

    :type start: str
    :param start: The starting item for the generator sequence

    '''
    current = start
    yield start
    while True:
        current = increment_item(current)
        yield current


def build_unique_sequence(sequence):
    '''
    Given an input sequence, returns the same sequence with characters
    appended such that every element in the returned sequence is unique

    :type sequence: list
    :param sequence: The sequence to make unique
    :rtype: list
    :return: The resultant unique sequence. EX: sequence = [0, 1, 2, 3, 1, 4, 2, 1], return = ['0A', '1A', '2A', '3A', '1B', '4A', '2B', '1C']

    '''
    key_set = dict([item, char_generator()] for item in set(sequence))
    return map(lambda item:'{}{}'.format(item, key_set[item].next()), sequence)

Which results in:

>>> build_unique_sequence([0, 1, 2, 3, 1, 4, 2, 1])
['0A', '1A', '2A', '3A', '1B', '4A', '2B', '1C']

Upvotes: 1

Raymond Hettinger
Raymond Hettinger

Reputation: 226256

>>> import string, collections

>>> c = collections.Counter()
>>> for x in [0, 1, 2, 3, 1, 4, 2, 1]:
        letter = string.letters[c[x]]
        c[x] += 1
        print '%s%s' % (x, letter)


0A
1A
2A
3A
1B
4A
2B
1C

Upvotes: 0

Related Questions