Sequential search is a search that searches for what you want to search from the beginning, and ends when it is found.
It also serves as a practice for function definition.
python
# coding: UTF-8
def giiko_search(TargetValue, List): #(Search value,Search target)
#Confirm that the second argument is a list type
if isinstance(List, list) == False:
print 'The argument is not a list'
return 0
#Make sure the List is not empty
if len(List) == 0:
print 'It's an empty list'
return 1
#look for
for i in range(len(List)):
if List[i] == TargetValue:
print str(i+1) + 'In the second' + str(TargetValue) + 'Kakunin! Was good'
return 2
print 'I couldn't find it'
return 3
List = ['Fred', 'Alex', 'Diana', 'Byron', 'Carol']
giiko_search('Diana', List)
The answer-matching Wikipedia is easier. It was introduced.
python
def search(list, x):
return x in list
The point is the in operator, which is True if the left x
is included in the right list
, False if it is not. Seems to return.
In other words, search (list, x)
is a function that returns two values, "yes" and "no".
Recommended Posts