Reputation: 123
I have a large number of iterations (~60000) on each I get 2 lists at a time.
I need to keep some information about each possible combination of items from first/second list. if a pair is repeated I want to count it - not add, that's why I think dictionaries are more suitable.
I can do it with nested loops - but it takes forever.
I thought of using a dictionary of dictionaries.
I can tell all possible values ahead so I started with a dictionary with empty values - I want to add them iteratively.
If this is the input on the first iteration
l1 = ['1','2']
l2 = ["A", "B"]
I expect to get:
{'1':{'A': [1, 0],
'B': [1, 0]}
'2':{'A': [1, 0],
'B': [1, 0]},
'3': {},
'4': {},
'5': {}}
}
and after a second iteration with:
l1 = ['1','3','5']
l2 = ['B','C']]
I expect to get:
{'1':{'A': [1, 0],
'B': [2, 0],
'C': [1, 0]}
'2':{'A': [1, 0],
'B': [1, 0]}
'3':{'B': [1, 0],
'C': [1, 0]},
'4':{},
'5':{'B': [1, 0],
'C': [1, 0]}
}
What's the most efficient way of doing it?
Upvotes: 0
Views: 31
Reputation: 16966
If your lists are in large size, then I would suggest linked list sparse matrix.
Try this!
l1_superset = ['1','2','3','4','5']
l2_superset = ['A','B','C','D','E']
M=sparse.lil_matrix(np.zeros((len(l1_superset),len(l2_superset))))
mapper_l1 = {k:idx for idx,k in enumerate(l1_superset)}
mapper_l2 = {k:idx for idx,k in enumerate(l2_superset)}
def update (l1,l2):
for i in l1:
for j in l2:
M[mapper_l1[i],mapper_l2[j]] = M[mapper_l1[i],mapper_l2[j]] + 1
l1 = ['1','2']
l2 = ["A", "B"]
update(l1,l2)
l1 = ['1','3','5']
l2 = ['B','C']
update(l1,l2)
Upvotes: 1