By Leon Smith


2020-03-26 02:42:04 8 Comments

I've got a bit of a complicated question (for my limited Python knowledge) around iterating through and checking an array for any missing values.

I have an array of key strings and I need to check the array contains all substrings in another array. If it doesn't, I need to output which are missing.

Example:

array1 = ['key/value/one123904', 'key/value/two342389', 'key/value/three234093']
array2 = ['one', 'two', 'three', 'four']

My ideal output would be to say all elements of array2 exist in array1 if they do, or else in the example above, to output No key for value: four

5 comments

@daichaoyang 2020-03-26 05:11:55

sum_array1 =""
for string1 in array1:
    sum_array1 = sum_array1 + string1 + ","
missing = [string2 for string2 in array2 if string2 not in sum_array1]
if missing:                                                                     
    for string in missing:                                                      
        print("No key for value: " + string)                                    
else:                                                                           
    print("All elements of array2 exist in array1")

@Andrick 2020-03-26 03:33:18

This is the method i was able to make for your question,

def missing(arr1, arr2):
    #arr1 is the array of strings to be searched
    #arr2 is the array of substrings
    notFound=""
    for i in arr2: # i = each element in array 2
        for j in arr1: # j = each element in array 1
            if i in j: # if substring of i is in an element in j
                break # moves onto next element in the array
            elif j == arr1[-1]: # if not found in the string, checks if  on the last item in the array.
                notFound = notFound+" "+i
    if notFound != "":
        print("No key for value:", notFound)
    else:
        print("all elements of array2 exist in array1")

@John Carter 2020-03-26 03:11:20

array1 = ['key/value/one123904', 'key/value/two342389', 'key/value/three234093']
array2 = ['one', 'two', 'three', 'four']


def does_match_in_array_of_string(key: str, search_list : list) -> bool:
    for item in search_list:
        if key in item:
            return True
    return False;


match_failures = [key for key in array2 if not does_match_in_array_of_string(key, array1)]

if len(match_failures):
    print(f'No key for values: {match_failures}')
else:
    print('All keys have values')

@John Carter 2020-03-26 03:12:30

I'm curious if anyone can find a reasonable way to do it with filter()

@RoadRunner 2020-03-26 03:26:25

Depends on how you define reasonable?

@John Carter 2020-03-26 14:15:05

My quick attempt was gross and far from any human definition of reasonable :)

@dspencer 2020-03-26 02:46:51

You could achieve this by iterating over your list of substrings, array2, and test whether any of the key strings in array1 contain this substring, i.e.:

for string in array2:
    if not any(string in key_string for key_string in array1):
        print("No key for value: " + string)
        break                                                                   
else:                                                                           
    print("All elements of array2 exist in array1")

In case you're not familiar with the else clause of for, this will be executed only when the loop exits normally, i.e. will not execute if break is used to terminate the loop early.

If you wanted to record all of the substrings which were not present:

missing = [string for string in array2                                          
           if not any(string in ks for ks in array1)]                           
if missing:                                                                     
    for string in missing:                                                      
        print("No key for value: " + string)                                    
else:                                                                           
    print("All elements of array2 exist in array1")

@ted 2020-03-26 02:53:22

In one line:

print(
    "No key for value(s): {}".format(
        " ".join([k for k in array2 if not any(k in v for v in array1) ])
    )
)

Or if you want to handle more properly the case of all values being present

no_match = [k for k in array2 if not any(k in v for v in array1) ]
print(
    "No key for value(s): {}".format(" ".join(no_match))
    if no_match
    else "All keys have values"
)

Related Questions

Sponsored Content

97 Answered Questions

[SOLVED] How can I remove a specific item from an array?

  • 2011-04-23 22:17:18
  • Walker
  • 6472457 View
  • 8045 Score
  • 97 Answer
  • Tags:   javascript arrays

40 Answered Questions

[SOLVED] For-each over an array in JavaScript

26 Answered Questions

[SOLVED] How to check if a string contains a substring in Bash

  • 2008-10-23 12:37:31
  • davidsheldon
  • 2015782 View
  • 2499 Score
  • 26 Answer
  • Tags:   string bash substring

38 Answered Questions

[SOLVED] How do I check whether a file exists without exceptions?

3 Answered Questions

50 Answered Questions

39 Answered Questions

[SOLVED] Loop through an array in JavaScript

10 Answered Questions

[SOLVED] Does Python have a string 'contains' substring method?

21 Answered Questions

[SOLVED] Checking if a key exists in a JavaScript object?

11 Answered Questions

[SOLVED] How do I get a substring of a string in Python?

Sponsored Content