0

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

4
  • Why not find2 = 'var1 var3' return : val1, val3? Commented Jul 24, 2022 at 9:40
  • because var3 doesnt comes after var1 Commented Jul 24, 2022 at 9:45
  • So we can NOT ask find2 = 'var1 var4'?! Commented Jul 24, 2022 at 10:02
  • we can but it shall not find anything Commented Jul 25, 2022 at 1:13

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

1 Comment

this works great friend if this array change like that how can i let it work arr = [('var1', 'val1', 'val1-2', 'val1-3'), ('var2', 'val2', 'val2-2', 'val2-3'), ('var3', 'val3', 'val3-2', 'val3-3'), ('var4', 'val4', 'val4-2', 'val4-3'), ('var1', 'val5', 'val5-2', 'val5-3'), ('var3', 'val6', 'val6-2', 'val6-3'), ('var5', 'val7', 'val7-2', 'val7-3'), ('var1', 'val8', 'val8-2', 'val8-3'), ('var3', 'val9', 'val9-2', 'val9-3'), ('var6', 'val10', 'val10-2', 'val10-3')]
1

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'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.