0

使用する

def compare_lsts(list1,list2):
    first_set = set(list1)
    second_set=set(list2)
    results =[x for x in list1 if x in list2]
    print(results)

実行するとcompare_lsts([1,2,3,4,5],[3,8,9,1,7])、両方のセットに含まれる数値が得られます[1,3]

ただし、リスト1に複数のリストを含めるようにすると、たとえばcompare_lsts([[1,2,3,4,5],[5,8,2,9,12],[3,7,19,4,16]],[3,7,2,16,19])、が与えられ[],[],[]ます。

list1のlistに使用し、その後にループの結果を示しました。私は明らかに自分が何をしているのかわかりません。

基本的に問題は、1つの静的リスト内のアイテムをできるだけ多くのリストとどのように比較するかということです。

4

3 に答える 3

1

まず第一に、あなたはすでにセットを使い始めているので、封じ込めをチェックするときより速いので、あなたは間違いなくそれらを使うべきです。また、セットにはすでにいくつかの便利な組み込み機能があるため、2つのリストを比較するために、セットを交差させるだけで、両方のリストにあるアイテムを取得できます。

>>> set1 = set([1, 2, 3, 4, 5])
>>> set2 = set([3, 8, 9, 1, 7])
>>> set1 & set2
{1, 3}
>>> list(set1 & set2) # in case you need a list as the output
[1, 3]

同様に、2つのセットの和集合を見つけて、いずれかのセットに含まれるアイテムを取得することもできます。

>>> set1 | set2
{1, 2, 3, 4, 5, 7, 8, 9}

したがって、list1のサブリストのいずれかにあるlist2のすべてのアイテムを検索する場合は、すべてのサブリストをlist2と交差させてから、それらすべての結果を結合することができます。

>>> sublists = [set([1, 2, 3, 4, 5]), set([5, 8, 2, 9, 12]), set([3, 7, 19, 4, 16])]
>>> otherset = set([3, 7, 2, 16, 19])
>>> intersections = [sublist & otherset for sublist in sublists]
>>> intersections
[{2, 3}, {2}, {16, 3, 19, 7}]
>>> union = set()
>>> for intersection in intersections:
        union = union | intersection
>>> union
{16, 19, 2, 3, 7}

次を使用して、もう少しうまく行うこともできますfunctools.reduce

>>> import functools
>>> functools.reduce(set.union, intersections)
{16, 19, 2, 3, 7}

同様に、これらの結果を実際に交差させたい場合は、次のようにすることもできます。

>>> functools.reduce(set.intersection, intersections)
set()

そして最後に、それらすべてを素晴らしい関数に詰め込むことができます。

def compareLists (mainList, *otherLists):
    mainSet = set(mainList)
    otherSets = [set(otherList) for otherList in otherLists]

    intersections = [mainSet & otherSet for otherSet in otherSets]
    return functools.reduce(set.union, intersections) # or replace with set.intersection

そして、次のように使用します。

>>> compareLists([1, 2, 3, 4, 5], [3, 8, 9, 1, 7])
{1, 3}
>>> compareLists([3, 7, 2, 16, 19], [1, 2, 3, 4, 5], [5, 8, 2, 9, 12], [3, 7, 19, 4, 16])
{16, 19, 2, 3, 7}

Note, that I replaced the order of the arguments in the function, so the main list (in your case list2) is mentioned first as that is the one the others are compared to.

于 2012-11-21T18:23:05.963 に答える
0

それが最善の方法かどうかはわかりませんが、次のようになります。

def flat(l):
    c_l = []
    for i in l:
        if isinstance(i,list):
            map(c_l.append,i)
        else:
            c_l.append(i)
    return c_l


def compare_lsts(a,b):
    if all([True if isinstance(x,list) else False for x in a]): #if there is sublists in a
        a = flat(a) #flats a

    if all([True if isinstance(x,list) else False for x in b]): #if there is sublists in b
        b = flat(b) #flats b


    return list(set(a) & set(b)) #intersection between a and b


print (compare_lsts([[1,2,3,4,5],[5,8,2,9,12],[3,7,19,4,16]],[3,7,2,16,19]) #[16, 3, 2, 19, 7])
于 2012-11-21T18:19:54.017 に答える
0

すべてのリストにある最初の要素を探している場合:

set(first).intersection(second, third) # fourth, fifth, etc...
>>> set([1, 2, 3]).intersection([2, 3, 4], [3, 4, 5])
set([3])

他のリストのいずれかにある最初の要素を探している場合:

>>> set([1, 2, 3]) & set([4]).union([5])
set([2])

したがって、単純な関数:

def in_all(fst, *rst):
    return set(fst).intersection(*rst)

def in_any(fst, *rst):
    it = iter(rst)
    return set(fst) & set(next(it, [])).union(*it)
于 2012-11-21T18:37:49.253 に答える