あなたの質問を正しく理解できれば、単一の重複した値だけでなく、一連の値を探す必要があります。つまり、 in say[1,2,3,4]
の重複が見つかります。[2,3,4]
[39,87,2,3,4]
インポートとテスト値
import itertools,pprint
from collections import defaultdict
l = ((1, 2, 3, 4, 5),
(6, 7, 8, 9, 10),
(11, 12, 13, 14, 15),
(16, 17, 18, 19, 20),
(21, 22, 23, 24, 25),
(26, 8, 9, 29, 30),
(31, 13, 14, 15, 35),
(17, 18, 19, 39, 40),
(41, 23, 24, 44, 45),
(46, 47, 48, 49, 50))
メインコード:
seen = defaultdict(dict)
for y,row in enumerate(l):
rowlen = len(row)
values = [ [ (row[i:k+1]) for (i,k) in zip(range(rowlen),range(e,rowlen,1))] for e in range(rowlen) ]
for valueGroup in values:
for x,value in enumerate(valueGroup):
seen[value]['count'] = seen[value].get('count',0) + 1
seen[value]['x-coOrd'] = x
seen[("R",y)][value] = True
for y in range(len(l)):
my_rows_vals = seen[("R",y)].keys()
for value in my_rows_vals:
if seen[value]['count'] > 1:
print "{0} repeated at ({1},{2})".format(value,seen[value]['x-coOrd'],y)
サンプルとして出力します(さらに出力がありました):
(13, 14) repeated at (1,6)
(14, 15) repeated at (2,6)
(13,) repeated at (1,6)
(13, 14, 15) repeated at (1,6)
(14,) repeated at (2,6)
(17, 18) repeated at (0,7)
(18, 19) repeated at (1,7)
(17,) repeated at (0,7)
(18,) repeated at (1,7)
(19,) repeated at (2,7)
(17, 18, 19) repeated at (0,7)
(23,) repeated at (1,8)
(24,) repeated at (2,8)
(23, 24) repeated at (1,8)
リストの理解ロジックは、この例に基づいて推論されました
l = [1,2,3,4]
len = 4
i:k
0:1 1:2 2:3 3:4 i = 0,1,2,len-e k = e,e+1,e+2,e+3 e = 0
0:2 1:3 2:4 i = 0,1,len-e k = e,e+1,e+2 e = 1
0:3 1:4 i = 0,len-e k = e,e+1 e = 2
0:4 i = len-e k = e e = 3
この方法は、個々の数字と一連の数字の両方をチェックし、一致に関与する両方の当事者を強調するため、他の回答とは異なります。