列表中的编号重合

时间:2018-03-13 16:26:12

标签: python list for-loop

我有一个包含以下结构的列表:

A1, B2, C3, 66
A1, B2, C3, 00
A2, B2, C3, 77
A3, B3, C4, 44
A4, B4, C5, 11
A4, B4, C5, 12
A4, B4, C5, 13

我需要枚举唯一的1-3列元素以获得如下输出:

A1, B2, C3, 66, 1
A1, B2, C3, 00, 2
A2, B2, C3, 77, 1
A3, B3, C4, 44, 1
A4, B4, C5, 11, 1
A4, B4, C5, 12, 2
A4, B4, C5, 13, 3

如您所知,我希望第四列中的序号以1-3列的唯一值排序。

在阅读说明后,我得出结论,我需要使用collections模块。这是正确的决定吗?

我试试这个:

new = ['A1,B2,C3,66','A1,B2,C3,00','A2, B2, C3, 77','A3, B3, C4, 44','A4, B4, C5, 11','A4, B4, C5, 12','A4, B4, C5, 13']
test=[]
i = 0
for a in new:
    i+=1
    test.append('{},{}'.format(i, a))
    print(test)
    if a[i]!=a[i-1]:
        continue

3 个答案:

答案 0 :(得分:1)

您可以使用itertools.groupby

import itertools
import re
new = ['A1,B2,C3,66','A1,B2,C3,00','A2, B2, C3, 77','A3, B3, C4, 44','A4, B4, C5, 11','A4, B4, C5, 12','A4, B4, C5, 13']
n = map(lambda x:re.split(',\s*', x), new)
s = [list(b) for _, b in itertools.groupby(n, key=lambda x:x[:-1])]
last_data = map(lambda x:', '.join(x[:-1]+[str(x[-1])]), [i for b in [[b+[i] for i, b in enumerate(c, start=1)] for c in s] for i in b])

输出:

['A1, B2, C3, 66, 1', 
'A1, B2, C3, 00, 2', 
'A2, B2, C3, 77, 1', 
'A3, B3, C4, 44, 1', 
'A4, B4, C5, 11, 1', 
'A4, B4, C5, 12, 2', 
'A4, B4, C5, 13, 3']

答案 1 :(得分:0)

如果你喜欢自己做List/Dictionnary/YourFavoriteICollectionImplementation,下面是一个选项(不是完美的解决方案)。

它使用一个名为group by的dict来存储一个键的出现次数。然后利用此dict在循环时获得预期值。

index_cache

输出:

import re
test = ['A1,B2,C3,66','A1,B2,C3,00','A2, B2, C3, 77','A3, B3, C4, 44','A4, B4, C5, 11','A4, B4, C5, 12','A4, B4, C5, 13']
index_cache = {} #store all occurrences count for each key

def prepare(data):
    formatted = re.split(',\s*', data.strip())
    index_cache['-'.join(formatted[:3])] = 0 # init the count to 0
    return formatted

test1 = list(map(prepare, test))

for item in test1:
    index_cache['-'.join(item[:3])] += 1
    item.append(index_cache['-'.join(item[:3])])
print (test1)

答案 2 :(得分:0)

无需转到itertools即可轻松编码。 itertools可以更快 - 但不知道,需要测试 - 但这更容易阅读/理解/操纵。您的枚举规则非常明确:对于名为new的列表中的每个元素,如果前三个项与列表中前一个元素的前三个相同,则将索引增加1。如果它们不同,请中断序列并从1开始。

以下是代码:

new = ['A1, B2, C3, 66','A1, B2, C3, 00','A2, B2, C3, 77','A3, B3, C4, 44','A4, B4, C5, 11','A4, B4, C5, 12','A4, B4, C5, 13']
result = []
prev = None
c = 1
for x in new:
    current = ", ".join(x.split(", ")[:3])
    if current == prev:
        c += 1
    else:
        c = 1
    result.append(", ".join((x, str(c))))
    prev = current