1

我在休息多年后重新开始编程,并且无法解决这个问题。用户正在定义分类集数量,但 RasterValue 列表的长度为 N

示例:ClassificationSet = (1, 2, 3, 4, 5, 6) #由用户定义
RasterVal = () #length = N

我使用 ClassificationSet 作为索引将项目存储到 RasterVal: RasterVal(ClassificationSet).add(Imported Value)

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

非常感谢您的帮助。我已经能够比较这些集合,但它列出了重复的值,即使它们出现在同一个集合列表中。

4

3 回答 3

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]

该算法的运行时间是线性的,而不是集合数量的二次方。

于 2012-05-11T19:28:55.570 回答
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])
于 2012-05-11T19:30:39.400 回答
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
于 2012-05-11T19:22:53.060 回答