Check if there is a list of "partial" rows in the list of complete rows

Sorry for the title, really could not come up with a simple way to explain this situation.

So, if I have one list of strings, for example:

list_1 = [ "cat", "rat", "mat" ]

How can I check that all of these lines are in another list that may have “fluff” around it (I mean, instead of saying “cat”, it might have “cat_mittens”, which would be nice, but "car_mittens" is not.

So for example:

list_A = [ "cat", "car", "cab" ]
list_B = [ "cat", "rat", "bat", "mat" ]
list_C = [ "cat_mittens", "rat", "mat" ]

Here, if I did an analysis on list_A, I would like False to return, for list_BI would need to return True, and for list_C, I would like True to return (since it contains all 3 lines of list A, even if "cat" has extra bits around it (which I called fuzz).

My current approach:

list_1 = [ "cat", "rat", "mat" ]
list_C = [ "cat_mittens", "rat", "mat" ]

temp_list = [False,] * 3

count = 0
for temp_1 in list_1:
  temp_list[ count ] = any( temp_1 in temp_2 for temp_2 in list_C )
  count += 1

result = all( temp_list )

There is added a complication that in my actual code all the lines in the C list should contain an additional line (for example, everyone would have to say "_filetype"), but this is less of a problem (I do this in the final line) all ").

, , , ( , , , . , , - , ), , .

?

, ! - , .

+4
4
def check_all(substrings, strings):
    """Check all *substrings* are in *strings*."""
    return all(any(substr in s for s in strings) for substr in substrings)

:

>>> list_1 = [ "cat", "rat", "mat" ]
>>> list_A = [ "cat", "car", "cab" ]
>>> list_B = [ "cat", "rat", "bat", "mat" ]
>>> list_C = [ "cat_mittens", "rat", "mat" ]
>>> check_all(list_1, list_A)
False
>>> check_all(list_1, list_B)
True
>>> check_all(list_1, list_C)
True
>>> check_all(list_1, ["ratcatmat"])
True
>>> check_all(["bc", "ab"], ["abc"])
True
+4

,

result = True
for s in list_1:
    result &= any(s in test_string for test_string in list_C)
print result

( ), :

def check(list_1, list_2):
    for s in list_1:
        if not any(s in test_string for test_string in list_C):
            return False
    return True

EDIT:

& =

x &= y

x = x & y 

x True, x AND y True

, y=True x=True, True, , , y=False, False

+2
count = 0
for item in list_1:
    for item2 in list_C:
        if item in item2:
            count += 1
            break
res = 'True' if count==len(list_1) else 'False'
print res
0
source

You can do it in python3. This should print the desired result.

list_1 = [ "cat", "rat", "mat" ]
list_2 = [ "cat_mittens", "rat", "mat" ]

for i in list_2:
    for j in list_1:
         if j in i:
            print(i) 

Hope this is what you want to do.

0
source

Source: https://habr.com/ru/post/1608018/


All Articles