Step #1 Presence verification of a value in a list
Considering a case when the task is to determine whether a searched value (s) is present in an array (or list) you could cycle through the list elements and verify by equality the presence. If a matching case is reached, the cycle may be stopped and return with positive feedback.
In Python(3) comments that are not part of the code start with # symbol. Here you find examples with integers, but this works for other simple variable types as well.
#initialization of variables a = [1, 2, 3, 4, 5, 10] #list of integers s = 4 #searched value for element in a: if element == s: answer = True return False # if there is no match
this answer may be printed out (on the screen) or better returned if we convert the above script to a function:
def check_presence(a, s):
for element in a:
if element == s:
return True
return False # if there is no match
and call like:
# initialize variables
a = [1, 2, 3, 4, 5, 10] #list of integers
s = 4
# call function with returned value
answer = check_presence(a, s)
Answer may be printed or used for decision in an if structure:
if check_presence(a, s):
#your code here
print('Defined value is present in the list')
Step #2 Absence verification
If the task is to verify the absence of given value, then the code is similar but now the True/False branches of the code are inversed.
def check_absence(a, s):
for element in a:
if element == s:
return False
return True # True, there is no match
Note that it is important to name the function appropriately and define True/False values accordingly. Inappropriate naming and wrong definition may lead to misunderstandings and functional but wrong code (logical/semantic error).
Step #3 Absence of list elements in another list
Now, if not only one value should be checked but a list of values then the elements of the list to be verified one-by-one. The function that cycles through elements of s (list) and searches in a (list):
def findMissingElements(a,s):
# elements of s are searched in elements of a
notin_list = [] # predefined empty list
for element in s:
if element not in a:
notin_list.append(element)
return notin_list
Now we may test the code with two lists, for example:
#initialization of lists
s = [1, 2, 3, 4, 5, 10] #list elements to check for
a = [2, 3, 1, 0, 5] #list to search in
#calling the function with return value
notin_list = findMissingElements(a,s)
print(notin_list)
This returns a list [4, 10] because 4 and 10 are the numbers present in list s but missing from list a.
We may consider that we are done, but this is not the fastest solution if occasionally a contains repeated values. In such case the following modification should be applied.
Step #4 verifying unique values only
Python has an option to create a list of unique values from a list, that is called a set. It is an inbuilt variable type and conversion has inbuilt module(s), so the process is fast. Conversion is faster then the time we may lose with verifying repeated values.
def findMissingElements(a,s):
# elements of s are searched in elements of a
notin_list = [] # predefined empty list
a_unique = set(a)
for element in s:
if element not in a_unique:
notin_list.append(element)
return notin_list
Note: that instead of predefining a_unique could have been replaced in the for cycle as:
if element not in set(a):
...
this is valid code but in such case the list -> set conversion is made at every step of the cycle, so that runtime would be way more than in the #3 case.
test FAST list element absence
No comments:
Post a Comment