[PYTHON] Is it possible to build a loop structure with a person whose city name and mayor's name are confusing?

Conclusion

** I can't assemble **.

The longest relay is

--Mayor of Awahara City, Chiba --Mayor Kumagaya of Chiba City --Mayor of Tomioka, Mr. Kumagai --Mayor Enomoto of Tomioka City

4 cities.

The above results are as of November 2020.

Introduction

image.png (Quoted from his twitter)

Chiba City's Mayor Kumagai (I'm about to become the prefectural governor) is famous, isn't it?

image.png (Quoted from Wikipedia)

However, the mayor of Kumagaya is likely to be confused with the mayor of ** Kumagaya City, Saitama Prefecture **. I can't help it because it's my last name, but sometimes this kind of name conflict occurs. Furthermore, theoretically, there is a possibility that an infinite loop of "YY mayor of XX city → ZZ mayor of YY city → XX mayor of ZZ city" may occur, which is difficult! You have to look it up.

Survey method

National Mayors Association Homepage has published the names of the mayors of all local governments (it seems that towns and villages are not included). I will search all. According to the specifications of the other page, the search result does not have a unique address, so if there is a strange person who wants to actually execute the code below, actually access the page and issue a search query to get the html file please. In the code below, that is source.html.

Source code below:

import re
import requests

def search(p,result):
    print('{}Of the city{}Searching for the mayor... '.format(p[0],p[1]))
    for r in result:
        if r[0] == p[1]:
            return 1+search(r,result)  
    print('{}The city doesn't seem to exist\n'.format(p[1]))
    return 1

def main():
    t = open('source.html').read()
    pattern = r'<tr><td class="city_info">(.*?)city</td><td class="city_info">(.*?) (.*?)</td><td class="city_info">'
    result = re.findall(pattern,t)
    max_length = 0
    for r in result:
        max_length = max(max_length,search(r,result))
        if max_length == 4:
            break
    return

main()

State of execution.

Searching for Mayor Noda of Kamaishi City...
Searching for Mayor Suzuki of Noda City...
Suzuki city doesn't seem to exist

Searching for Mayor Fujiwara of Ninohe...
Fujiwara City doesn't seem to exist

Searching for Mayor Tamura of Hachimantai City...
Searching for Mayor Honda of Tamura City...
Honda city does not seem to exist

Searching for Mayor Ozawa of Oshu City...
Ozawa city doesn't seem to exist

Searching for Mayor of Takizawa City...
Main Hama City does not seem to exist

Searching for the mayor of Sendai City...
The county city doesn't seem to exist

Searching for Mayor Kameyama of Ishinomaki City...
Searching for Mayor Sakurai of Kameyama City...
Sakurai City doesn't seem to exist

Searching for Mayor Sato of Shiogama City...
Sato city doesn't seem to exist

Searching for Mayor Sugawara of Kesennuma...
Sugawara City doesn't seem to exist

Searching for Mayor Yamada of Shiroishi City...
Yamada City does not seem to exist

Searching for Mayor Yamada of Natori City...
Yamada City does not seem to exist

Searching for Mayor Kurosu of Kakuda City...
Black Su City doesn't seem to exist

Searching for Mayor Fukaya of Tagajo City...
Searching for Mayor Kojima of Fukaya...
Kojima City doesn't seem to exist

Searching for Mayor Kikuchi of Iwanuma City...
Kikuchi City doesn't seem to exist

Searching for Mayor Kumagaya of Tome City...
Searching for Mayor Tomioka of Kumagaya...
Searching for Mayor Enomoto of Tomioka City...
Enomoto City doesn't seem to exist

Searching for the mayor of Chiba in Kurihara...
Searching for Mayor Kumagaya of Chiba City...
Searching for Mayor Tomioka of Kumagaya...
Searching for Mayor Enomoto of Tomioka City...
Enomoto City doesn't seem to exist

result

If you had a loop structure, this code would cause an infinite loop and output infinite lines, but fortunately or unfortunately it didn't. When I checked the maximum length, it was four. I thought I would go about 6 but it's unexpectedly few ...

By the way, the four lines are ** Awahara City → Chiba City → Kumagaya City → Tomioka City Line ** shown at the beginning. However, this line is expected to collapse because the mayor of Chiba City, Toshihito Kumagai, will run for the prefectural governor and will probably be elected. The realization of the mayor name loop seems to be far away.

Recommended Posts

Is it possible to build a loop structure with a person whose city name and mayor's name are confusing?
Is it possible to enter a venture before listing and make a lot of money with stock options?
[Python] What is a tuple? Explains how to use without tuples and how to use it with examples.
Is it possible to detect similar images only with ImageHash?
Make it possible to output a log to a file with go echo
How to make a container name a subdomain and make it accessible in Docker
Read CSV file with Python and convert it to DataFrame as it is
Make a decision tree from 0 with Python and understand it (4. Data structure)
A command to specify a file with a specific name in a directory with find and mv, cp, or gzip it (linux)