Sequential search with Python

Sequential search

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)

Execution result

in operator

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

Sequential search with Python
Binary search with python
Binary search with Python3
Full bit search with Python
Search engine work with python
Search twitter tweets with python
Streamline web search with python
Learn search with Python # 2bit search, permutation search
FizzBuzz with Python3
Scraping with Python
Statistics with python
Scraping with Python
Python with Go
Twilio with Python
Integrate with Python
Play with 2016-Python
AES256 with python
Tested with Python
python starts with ()
with syntax (Python)
Bingo with python
Zundokokiyoshi with python
Excel with Python
Microcomputer with Python
Cast with python
Algorithm learned with Python 10th: Binary search
Algorithm learned with Python 9th: Linear search
Search the maze with the python A * algorithm
Algorithm learned with Python 12th: Maze search
Serial communication with Python
Django 1.11 started with Python3.6
Primality test with Python
Python with eclipse + PyDev.
Socket communication with Python
Data analysis with python 2
Scraping with Python (preparation)
Try scraping with Python.
Csv output from Google search with [Python]! 【Easy】
Automatically search and download YouTube videos with Python
Learning Python with ChemTHEATER 03
"Object-oriented" learning with python
Run Python with VBA
Handling yaml with python
Solve AtCoder 167 with python
Serial communication with python
[Python] Use JSON with Python
Learning Python with ChemTHEATER 05-1
Learn Python with ChemTHEATER
Run prepDE.py with python3
python memorandum (sequential update)
Causal reasoning and causal search with Python (for beginners)
1.1 Getting Started with Python
Collecting tweets with Python
Python Exercise 1-Breadth-first search
Binarization with OpenCV / Python
[Python] Search (itertools) ABC167C
3. 3. AI programming with Python
Binary search in Python
Kernel Method with Python
Non-blocking with Python + uWSGI
Scraping with Python + PhantomJS