i have a python list of lists i want to merge all the containing list with at least 1 common element and remove the similar items
i have a big set of data which is a list of lists, with some common data in some of the containing lists, i want to merge all lists with common data
JavaScript
x
21
21
1
# sample data
2
foo = [
3
[0,1,2,6,9],
4
[0,1,2,6,5],
5
[3,4,7,3,2],
6
[12,36,28,73],
7
[537],
8
[78,90,34,72,0],
9
[573,73],
10
[99],
11
[41,44,79],
12
]
13
14
# i want to get this
15
[
16
[0,1,2,6,9,5,3,4,7,3,2,78,90,34,72,0],
17
[12,36,28,73,573,73,573],
18
[99],
19
[41,44,79],
20
]
21
the elements containing even one common element they are grouped together
the original data file is this
Edit
this is what i am trying
JavaScript
1
26
26
1
import json
2
3
data = json.load(open('x.json')) # https://files.catbox.moe/y1yt5w.json
4
5
6
class Relations:
7
def __init__(self):
8
pass
9
10
def process_relation(self, flat_data):
11
relation_keys = []
12
rel = {}
13
for i in range(len(flat_data)):
14
rel[i] = []
15
for n in flat_data:
16
if i in n:
17
rel[i].extend(n)
18
return {k:list(set(v)) for k,v in rel.items()}
19
20
def process(self, flat_data):
21
rawRelations = self.process_relation(flat_data)
22
return rawRelations
23
24
rel = Relations()
25
print(json.dumps(rel.process(data), indent=4), file=open('out.json', 'w')) # https://files.catbox.moe/n65tie.json
26
NOTE – the largest number present in the data will be equal to the length of list of lists
Advertisement
Answer
A simple (and probably non-optimal) algorithm that modifies the input data in place:
JavaScript
1
15
15
1
target_idx = 0
2
3
while target_idx < len(data):
4
src_idx = target_idx + 1
5
did_merge = False
6
while src_idx < len(data):
7
if set(data[target_idx]) & set(data[src_idx]):
8
data[target_idx].extend(data[src_idx])
9
data.pop(src_idx) # this was merged
10
did_merge = True
11
continue # with same src_idx
12
src_idx += 1
13
if not did_merge:
14
target_idx += 1
15