Y
Y
Yrets1692021-11-24 18:35:59
Python
Yrets169, 2021-11-24 18:35:59

How to compare two lists with partial match of elements?

Good afternoon, how can I compare two lists with a partial match on the elements?

trying to do so

files = ['новая папка', 'games', 'install', 'фото', 'музыка']
search = ['папка', 'фотки', 'музло']
result=list(set(search) & set(files))
print(result)

the result will be if both lists have the same elements

----------------------------------------- -------------------------------------------------- ----------------------------------------------------

expected result for a partial match elements between files and search:
['new folder', 'photo', 'music']

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vlad Grigoriev, 2021-11-24
@Yrets169

it will be crooked and not entirely correct, but there is no simple solution here, you have to take and look for semantic comparison

import re

files = ['новая папка', 'games', 'install', 'фото', 'музыка']
search = ['папка', 'фотки', 'музло']

finds = set()
chars_find = re.compile(r'\w{3}')

for file in files:
    for world_search in search:
        for world in chars_find.findall(world_search):
            if world in file:
                finds.add(file)

print(finds)

V
Vindicar, 2021-11-24
@Vindicar

Set operations to the rescue, intersection() in particular.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question