I am trying to find multiple values in python array list in order and merge returned data.
arr = [('var1','val1'),('var2','val2'),('var3','val3'),('var4','val4'),('var1','val5'),('var3','val6'),('var5','val7'),('var1','val8'),('var3','val9'),('var6','val10')]
find1 = 'var1'
this shall return val1
find2 = 'var1 var3'
this shall return val5, val6
find2 = 'var1 var3 var6'
this shall return val8, val9, val10
I couldnt find any example about finding multiple values in order, so needed to open this question
thanks
CodePudding user response:
One solution:
arr = [('var1', 'val1'), ('var2', 'val2'),
('var3', 'val3'), ('var4', 'val4'),
('var1', 'val5'), ('var3', 'val6'),
('var5', 'val7'), ('var1', 'val8'),
('var3', 'val9'), ('var6', 'val10')]
finds = ['var1', 'var1 var3', 'var1 var3 var6']
keys, values = zip(*arr)
for find in finds:
b = tuple(find.split())
# find the first index of the matching sequence
index = next(i for i in range(len(arr)) if keys[i:i len(b)] == b)
# join the result from the matching index to the required length
res = " ".join(values[index:index len(b)])
print(res)
Output
val1
val5 val6
val8 val9 val10
As an alternative use difflib.SequenceMatcher
:
from difflib import SequenceMatcher
arr = [('var1', 'val1'), ('var2', 'val2'),
('var3', 'val3'), ('var4', 'val4'),
('var1', 'val5'), ('var3', 'val6'),
('var5', 'val7'), ('var1', 'val8'),
('var3', 'val9'), ('var6', 'val10')]
finds = ['var1', 'var1 var3', 'var1 var3 var6']
keys, values = zip(*arr)
for find in finds:
b = find.split()
s = SequenceMatcher()
s.set_seqs(keys, b)
match = s.find_longest_match()
res = " ".join(values[match.a:match.a match.size])
print(res)
Output
val1
val5 val6
val8 val9 val10
CodePudding user response:
I would recommend transforming your list into the dictionary:
dct = {i[0]:i[1] for i in arr}
Then use this function to return multiplies values from your dictionary as your described in your question:
def findMultiple(dct, find):
find = find.split(" ")
results = [dct[key] for key in find]
return " ".join(results)
Usage:
>>> findMultiple(dct, "var1")
'val8'
>>> findMultiple(dct, "var1 var3")
'val8 val9'
>>> findMultiple(dct, "var1 var3 var6")
'val8 val9 val10'