Reputation: 51
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
Upvotes: 0
Views: 2899
Reputation: 31
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")
Upvotes: 0
Reputation: 11
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")
Upvotes: 1
Reputation: 6995
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')
Upvotes: 0
Reputation: 4471
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")
Upvotes: 2
Reputation: 14734
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"
)
Upvotes: 0