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


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-list上,因此比以前要贵一些。

根据注释,这里有一个可能的解决方案。也许可以通过使用itertools.groupby或其他技巧来缩短它。

def find_group(l, group_len):
i = 0
while i < len(l):
j = i + 1
while j < len(l) and l[i] == l[j]:
j += 1
if j - i == group_len:
return i
i = j
return None
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']
start3 = find_group(a, 3)
start5 = find_group(a, 5)
c = []
d = []
if start5 is not None:
c += a[start5:start5 + 5]
d += b[start5:start5 + 5]
if start3 is not None:
c += a[start3:start3 + 3]
d += b[start3:start3 + 3]
print(c)
print(d)

相关内容

最新更新