[英]Why is Python concatenating strings in a list rather than iterating over them?
[英]iterating over list and concatenating alphabetical sequence in Python
我试图遍历一个列表,并为列表中的每个元素分配一个字母字母,如果有重复,则向其分配下一个字母以获取唯一项。
sequence = [0, 1, 2, 3, 1, 4, 2, 1]
unique_seq = [0A, 1A, 2A, 3A, 1B, 4A, 2B, 1C]
我试图生成这样的字母列表:
alpha = list(map(chr, range(65, 91)))
然后我想遍历这样的序列:
for i in sequence:
unique_seq.append(i) for i in sequence if i not in unique_seq else...
我不确定如何进行其余工作...
谢谢,
这是一个解决方案,可以处理无限大小的序列和无限次重复(允许内存)
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)
结果是:
>>> build_unique_sequence([0, 1, 2, 3, 1, 4, 2, 1])
['0A', '1A', '2A', '3A', '1B', '4A', '2B', '1C']
>>> 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
我将其作为发电机。 我正在使用集合中的Counter数据结构。 如果键尚不存在,则返回0,而不是键错误。 因此,我们可以将其用作alpha列表的索引。
添加了使excel列样式字母的功能。 测试了多达一百万个相同版本的版本。
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)
输出:
0A
1A
2A
3A
1B
...
1AC
1AD
1AE
注意:Python 3语法。 根据需要更改打印和划分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.