在Python中遍历列表和连接字母序列

2024-04-27 00:18:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图遍历一个列表,并为列表中的每个元素分配一个字母,如果有一个重复的元素,那么将字母表中的下一个字母分配给它以获得唯一的项。在

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

我试着生成如下字母表:

^{pr2}$

然后我想像这样迭代序列:

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

我不知道接下来该怎么办。。。在

谢谢你


Tags: in元素列表forif字母not序列
3条回答
>>> 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

我把它做成发电机。我使用的是collections中的Counter数据结构。如果键不存在,则返回0,而不是键错误。所以我们可以用它作为alpha列表的索引。在

添加了一个函数来生成excel列样式的字母。测试了多达100万个相同数量的版本。在

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)

输出:

^{2}$

注意:Python3语法。根据需要更改打印和分区。在

这里有一个解决方案,可以处理无限大的序列和无限的重复次数(允许内存)

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)

结果是:

^{2}$

相关问题 更多 >