2017-05-29 4 views
0

同じ要素を持つサブリストをグループ化する必要があり、サブリストのすべての要素にはlist2という要素はなく、要素はlist3 です。サブリストにはlist2に要素がなく、list3に要素があります

list1 = [[1,4],[4,5],[5,7],[6,7],[9,7],[10,9],[8,10],[8,11],[8,13],[13,15]] 
list2 = [7,8] 
list3 = [1,6,11,13] 

、どちらも1と同じ番号が含まれているので、私は一緒に[4,5][1,4]をリンクすると、これらの二つは[1,4,5]に結合するだろうと、彼らはlist31を含み、list2

7を含みません

のでリンクした後、新たなリストは次のようにする必要があります:

new_list1 = [[1,4,5],[6],[11],[13,15]] 

IE:サブリストと注文内で同じ番号があってはならない重要ではありません。

長い例:

list1 = [[1,4],[4,5],[5,7],[6,7],[9,7],[10,9],[8,10],[8,11],[8,13],[6,8],[20,2],[20,3],[11,14],[14,16],[13,15]] 
list2 = [7,8,20] 
list3 = [1,2,3,16,15] 

リンク後、それがどのようにこれは一般的な方法で

new_list = [[1,4,5],[2,3],[11,14,16],[13,15]] 

を行うことができますでしょうか? 最終的アルゴリズムは、次の3つの基本ステップで構成する必要があります

EDIT:

  1. list2
  2. に含まれているlist1のすべてのサブリストのすべての要素を削除し、すべてのサブリストに参加list1共通要素を持つ
  3. list1のサブリストのうち、いずれの要素も含まないすべてのサブリストを削除します。list3
+3

あなたが希望する結果を得るためにリンクされているリストする私には明確ではありません。あなたの最初の例では、 '[1,4]'はどこから来ますか? –

+0

申し訳ありませんが、それをリスト1に修正しました。 – jack

+0

試しましたか? – Nuageux

答えて

2

はここでトーマス・クーンが適切にあなたの心を読むことに成功した場合、それが私の感想です

list1 = [[1, 4], [4, 5], [5, 7], [6, 7], [7, 8], [9, 7], [10, 9], [8, 10], [8, 11], [8, 13], [13, 15]] 
list2 = [7, 8] 
list3 = [1, 6, 11, 13] 

print(subgroup_join(list1, list2, list3)) 
# prints: [[1, 4, 5], [6], [11], [13, 15]] 

list1 = [[1, 4], [4, 5], [5, 7], [6, 7], [9, 7], [10, 9], [8, 10], [8, 11], [8, 13], [6, 8], [20, 2], [20, 3], [11, 14], [14, 16], [13, 15]] 
list2 = [7, 8, 20] 
list3 = [1, 2, 3, 16, 15] 

print(subgroup_join(list1, list2, list3)) 
# prints: [[1, 4, 5], [2], [3], [11, 14, 16], [13, 15]] 

これはおそらく提示から最速のアプローチですが、再び - それはあなたの例と正確に一致しません - 最後の結果セットと[2][3]の結果を確認してください。

UPDATE

それが2番目のリストのグループを使用して、パフォーマンスに来る:

zwer_join - 100,000 loops: 2.849 s; per loop: 28.399 µs 
kuhn_join - 100,000 loops: 3.071 s; per loop: 30.706 µs 
nuag_join - 1,000 loops: 15.82 s; per loop: 15.819 ms (had to reduce the number of loops) 
+0

非常にうまく構成されています。あなたは間違いなく私よりも多くの練習をセットで持っています:) –

0

まず、join関数を記述します。不要な要素を削除するために2番目のリストを追加しました。

次に、結合されたリストを繰り返して、現在リストに含まれている要素があるかどうかを調べます。はいの場合、あなたが所属する場所を探し、次に要素を追加します(重複を避けるためにsetを使用しました)。

出力は最後に表示されます。

def join(list1, list2): 
    l = [] 
    for ee in list1: 
     # We consider here that list1 only have pairs 
     if ee[0] not in list2 and ee[1] not in list2: 
      flat_l = [x for e in l for x in e] 
      if ee[0] in flat_l or ee[1] in flat_l: 
       for i, e in enumerate(l): 
        if ee[0] in e: 
         l[i].append(ee[1]) 
        if ee[1] in e: 
         l[i].append(ee[0]) 
      else: 
       l.append(ee) 
    return l 

def f(list1,list2,list3): 
    l = [[e] for e in list3] 
    list1 = join(list1, list2) 
    for ee in list1: 
     flat_l = [x for e in l for x in e] 
     for e in ee: 
      if e in flat_l: 
       for i in range(len(l)): 
        if e in l[i]: 
         l[i] = list(set(l[i]+ee)) 
    print(l) 

list1 = [[1,4],[4,5],[5,7],[6,7],[9,7],[10,9],[8,10],[8,11],[8,13],[13,15]] 
list2 = [7,8] 
list3 = [1,6,11,13] 

f(list1,list2,list3) 
# [[1, 4, 5], [6], [11], [13, 15]] 

list1 = [[1,4],[4,5],[5,7],[6,7],[9,7],[10,9],[8,10],[8,11],[8,13],[6,8],[20,2],[20,3],[11,14],[14,16],[13,15]] 
list2 = [7,8,20] 
list3 = [1,2,3,16,15] 

f(list1,list2,list3) 
# [[1, 4, 5], [2], [3], [16, 11, 14], [13, 15]] 
+0

それを速くすることは可能ですか? – jack

+0

おそらく、準備ができている努力と時間に依存します。それでも、あなたは遅くなる 'append()'に依存するでしょう。 – Nuageux

1

このコードは、ジョブ実行する必要があります。最初の例

list1 = [[1,4],[4,5],[5,7],[6,7],[9,7],[10,9],[8,10],[8,11],[8,13],[6,8],[20,2],[20,3],[11,14],[14,16],[13,15]] 
list2 = [7,8,20] 
list3 = [1,2,3,16,15] 

list1a = [set(l) for l in list1] 
#removing elements from list1 that contain numbers of list2: 
for x in list2: 
    for l in list(list1a): 
     if x in l: 
      l.remove(x) 

#joining sub-lists in list1: 
list1b = [set(l) for l in list1a] 
list1c = [] 
while list1b: 
    s1 = list1b.pop(0) 
    for s2 in list(list1b): 
     if s1 & s2: 
      s1 |= s2 
      list1b.remove(s2) 
    list1c.append(s1) 

#generating final list with only sub-lists that contain elements of list2 
list1_new = sorted([sorted(list(s)) for s in list1c if s & set(list3)]) 

を、これは与える:

[[1, 4, 5], [6], [11], [13, 15]] 

と第二例えば

[[1, 4, 5], [2], [3], [11, 14, 16], [13, 15]] 

・ホープ、この助けてください。テスト用として

def subgroup_join(data, exclude, include): 
    exclude = set(exclude) # turn into set for faster lookup/compare 
    include = set(include) # turn into set for faster lookup/compare 
    data = [set(element) - exclude for element in data] # remove excluded elements 
    results = [set()] # init with an empty set 
    for element in data: # loop through our remaining elements 
     groups = [] # store elements/current results filtered by exclude list 
     ignore_element = False # flag if we should add the element as a standalone 
     for result in results: # go through each subgroup in the results 
      if element & result: # if the current element has common items with the result 
       result |= element # ... concatenate both into a subgroup 
       ignore_element = True 
      groups.append(result) # add the current result subgroup 
     if not ignore_element: # add only if the element wasn't concatenated 
      groups.append(element) # add the current element 
     results = groups # our element store becomes our new results set 
    return sorted([sorted(res) for res in results if result & include]) # sort & return 

+0

2番目の結果セットは、彼の例で示されているように単数の[2,3]を生成しません。 – zwer

+0

@zwer私は知っています。 '20'が '禁止されたリスト'にあるので、' [20,2] 'と' [20,3] 'は結合できません。それは尋問者の間違いでなければならない。 –

+0

私はそれを知っています。なぜそれが正しい結果をもたらさないのか知っていますが、アルゴリズムに何かがあるか、質問に間違いがあります。この時点では、彼の問題を説明するOPの能力を説明します。 – zwer

関連する問題