2012-05-11 91 views
1

經過幾年的關注,我回到了編程階段,並且在繞過這個問題時遇到了麻煩。用戶定義的分類設定量但RasterValue是列表是長度N如何比較重複項目列表(含有N項),但僅限於不同列表中的項目

示例: ClassificationSet =(1,2,3,4,5,6)由用戶#define的
RasterVal =()#長度= N

我存儲物品進入RasterVal使用ClassificationSet作爲指標: RasterVal(ClassificationSet)。新增(導入值)

RasterVal(1) = 22,23,24,25,23,23,22 
RasterVal(2) = 22,30,31,32,30,30 
RasterVal(3) = 31 

它是:RasterVal([],[22,23,24 ,25,23,23,22],[22,30,31,32,30,30],[31])

我想列出重複的值,但只有當它們在不同的集合中重複時纔是重複的,而不是相同的。

輸出應該是: RepeatSet = 22,31

非常感謝您的任何幫助。我已經能夠比較這些集合,但是它列出了重複的值,即使它們出現在相同的集合列表中。

+1

你可以嘗試使用'set'和'Counter'來完成它。 – lukecampbell

+0

相關:http://stackoverflow.com/questions/2116286/python-find-identical-items-in-multiple-lists – ChristopheD

回答

4

@lukecampbell是正確的:

>>> lsts = [[22,23,24,25,23,23,22],[22,30,31,32,30,30],[31]] 
>>> from collections import Counter 
>>> c = Counter(x for lst in lsts for x in set(lst)) 
>>> [x for x,y in c.items() if y > 1] 
[22, 31] 

該算法的運行時間是線性的,而不是在組數二次的。

0
#-- Your initial data... 
rasterVals = [ 
    (22,23,24,25,23,23,22), 
    (22,30,31,32,30,30), 
    (31,) 
] 

#-- This will be a set, which holds only distinct values. 
repeated = set() 

#-- We set up a classic inner-outer loop to iterate over all 
#-- pairs of lists in your given data. (Note, there are better ways 
#-- of doing this in Python, but this is the easiest to read and understand) 
for a in rasterVals: 
    #-- First, we'll make a set from the first element. 
    #-- Since sets only hold distinct values, any repeated numbers 
    #-- in the sequence will be ignored. 
    s = set(a) 

    for b in rasterVals: 
     #-- Test that we are not comparing a sequence to itself. 
     if (a is not b): 
      #-- Make a set for the inner loop. (again, only distinct numbers...) 
      t = set(b) 

      #-- Get the intersection of s and t, answering with only those elements 
      #-- that are in both s and t. 
      intersecting = (s & t) 

      #-- We update our set of (inter-sequence) repeated elements. 
      repeated.update(intersecting) 

#-- Show the results. 
print repeated 
2
RasterVal = [[22,23,24,25,23,23,22],[22,30,31,32,30,30],[31]] 

from itertools import combinations 
out = set() 
for a,b in combinations(RasterVal,2): 
    out = out|(set(a)&set(b)) 

print out 
#output: 
set([22, 31])