简体   繁体   中英

Python splitting lists to intersections iteratively

What I am trying to do is splitting some lists, based on their common elements, with an identifier of which list they came from. So, first build a list that contains all of the common items in all lists, then a list with those existing in a subset of lists etc.

It's better to explain it with an example:

list A : [ 2, 4, 6, 8, 10 ]
list B : [ 2, 6, 10, 11 , 13 ]
list C : [ 3, 6, 8, 9 , 11 ]
===> OUTPUT :
( [ A, B, C ] : [6] ) ,
( [ A, B ] : [ 2, 10 ] ) ,
( [ A, C ] : [ 8 ] ) 
( [ B, C ] : [ 11 ] ) ,
( [ A ] : [ 4 ] ) ,
( [ B ] : [ 13] ) ,
( [ C ] : [ 3 , 9 ]

I can find a way of working through that in paper :

  • find interestion A&B , then remove these items from A and B. A&B = [2,6,10], A = [ 4,8] , B = [ 11,13 ]
  • then find interection of A&B&C = [ 6 ] , and now A&B becomes [ 2, 10 ] .
  • continue like this iteratively and every time update my lists, with their new content etc.

But the above does not seem so pythonic to me - and I am a fairly new user to Python. Can I use any libraries to help me do so? I am planning to use python 2.7 but if going to Python 3 can help me I can do so.

are u on programming competition or something ?

>>> A=[ 2, 4, 6, 8, 10 ]
>>> B =[ 2, 6, 10, 11 , 13 ]
>>> C =[ 3, 6, 8, 9 , 11 ]
>>> r=set(A+B+C)
>>> l=[[] for i in r]
>>> for index,i in enumerate(r):
    add=[]
    if i in A:
        add.append("a")
    if i in B:
        add.append("b")
    if i in C:
        add.append("c")
    l[index].extend(add)


>>> d={}
>>> for i in r:
    d[i]=0


>>> for index,i in enumerate(d):
    d[i]=l[index]


>>> final={}
>>> for i in d.values():
    final[tuple(i)]=[]


>>> for k,v in d.items():
    final[tuple(v)].append(k)


>>> final
{('c',): [3, 9], ('a',): [4], ('a', 'c'): [8], ('a', 'b'): [2, 10], ('a', 'b', 'c'): [6], ('b', 'c'): [11], ('b',): [13]}
>>> 

I don't have enough reputation to comment, and I'm sure this is not the best way to do it, but a quick thing that maybe can help.

If you start with a dictionary that associates each list to a letter:

lists = {
 'A' : [2,4,6,8,10],
 'B' : [2,6,10,11,13],
 'C' : [3,6,8,9,11]
 }

You can then map the occurrences with something similar to this (if you're not familiar with defaultdict or the collections library check it out, it's awesome)

from collections import defaultdict

occurrences = defaultdict(list)

for letter, values in lists.items():
    for value in values:
        occurrences[value].append(letter)

then you use another dict to group the occurrences dict by values

result = defaultdict(list)
for value, letters in occurrences.items():
    result[tuple(letters)].append(value)

and you get

 {('A',): [4],
  ('A', 'B'): [2, 10],
  ('A', 'B', 'C'): [6],
  ('A', 'C'): [8],
  ('B',): [13],
  ('B', 'C'): [11],
  ('C',): [3, 9]}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM