Skip to main content
 首页 » 编程设计

Python 3.6之不要使用 Counter 在列表中包含重复值

2025年02月15日9mfryf

a = ['1','1','1','1','1','3','3','3','1','1','1','1','1'] 
b = ['4','8','3','1','7','6','4','9','8','3','2','5','2'] 
 
from collections import Counter 
counts = Counter(a) 
 
c = [] 
d = [] 
 
for x, y in zip(a, b): 
    if counts[x] == 5: 
        c.append(x) 
        d.append(y) 
    if not counts[x] == 5: 
       if counts[x] == 3: 
           c.append(x) 
           d.append(y) 
    else: 
        None 

最初,代码会遍历列表 A,然后计算是否有重复 5 次的数字。如果出现 5 次,代码将附加列表 A 中的数字和对应的列表 B 值。

如果列表 A 中的数字出现 3 次,则将应用另一个条件,将打印列表 B 中的值和对应的值。

输出

# c = ['3','3','3'] 

但是在我的示例中,我认为如果稍后重复出现,它会在搜索 5 次出现时感到困惑。

期望的输出

# c = ['1','1','1','1','1','3','3','3'] 

我如何让它指定它必须恰好在一个实例中出现 5 次?


a = ['1','1','1','1','1','3','3','3','1','1','1','1','1'] 
b = ['4','8','3','1','7','6','4','9','8','3','2','5','2'] 
 
counter, c, d = 1, [], [] 
 
a[-1] = ['-1'] 
b[-1] = ['-1'] 
 
for i,j in enumerate(a): 
    if a[i-1]==a[i]: 
        counter+=1 
 
        if counter == 5 and a[i+1]!=a[i]: 
            c += [j]*5 
            d += b[i-4:i+1] 
            counter = 1 
 
        if counter == 3: 
            c += [j]*3 
            d += b[i-2:i+1] 
            counter = 1    
    else: 
        pass 

请您参考如下方法:

我想你可以使用一个简单的计数器,像这样:

#!/usr/bin/env python 
 
a = ['1','1','1','1','1','3','3','3','1','1','1','1','1'] 
b = ['4','8','3','1','7','6','4','9','8','3','2','5','2'] 
 
counter, c, d = 1, [], [] 
 
# append non existing values in a,b such that not to count the  
# last consecutive series - in this case the '1's 
# if you want to count the last 5 '1's then you can change the 
# following lines to: 
# a += ['-1'] and b += ['-1'] 
 
a[-1] = ['-1'] 
b[-1] = ['-1'] 
 
for i,j in enumerate(a): 
    if a[i-1]==a[i]: 
        counter+=1 
 
        if counter == 3 and a[i+1]!=a[i]: 
            c += [j]*3 
            d += b[i-2:i+1] 
            counter = 1 
 
        elif counter == 5: 
            c += [j]*5 
            d += b[i-4:i+1] 
            counter = 1    
    else: 
        pass 
 
print 'c list: {}'.format(c) 
print 'd list: {}'.format(d) 

这将产生以下结果:

c list: ['1', '1', '1', '1', '1', '3', '3', '3'] 
d list: ['4', '8', '3', '1', '7', '6', '4', '9'] 

更新

如果您希望您的 c 列表不包含重复值,请将上面的代码更改为:

#!/usr/bin/env python 
 
a = ['1','1','1','1','1','3','3','3','1','1','1','1','1','4','4','4','2'] 
b = ['4','8','3','1','7','6','4','9','8','3','2','5','2','1','2','3','1'] 
 
counter, c, d = 1, [], [] 
 
a[-1] = ['-1'] 
b[-1] = ['-1'] 
 
for i,j in enumerate(a): 
    if a[i-1]==a[i] and a[i] not in c: 
        counter+=1 
 
        if counter == 3 and a[i+1]!=a[i]: 
            c += [j]*3 
            d += b[i-2:i+1] 
            counter = 1 
 
        elif counter == 5: 
            c += [j]*5 
            d += b[i-4:i+1] 
            counter = 1    
    else: 
        pass 
 
print 'c list: {}'.format(c) 
print 'd list: {}'.format(d) 

结果:

c list: ['1', '1', '1', '1', '1', '3', '3', '3', '4', '4', '4'] 
d list: ['4', '8', '3', '1', '7', '6', '4', '9', '1', '2', '3'] 

请记住,通过这种方式,您的代码会进行更多检查以查看该项目是否在 c 列表中,因此比以前贵一些。