[Python] Output battles and combinations (nesting for statements and if statements)

Using for statement nesting and if statements in Python I tried to output the combination of professional baseball games.

If you have the concept of home, visitor

Most of the time in professional baseball Yakult vs. Hanshin If it is the notation, it is written first It often refers to holding at the Yakult headquarters (Meiji Jingu Stadium).

(Example) Yakult vs. Hanshin (Jingu Stadium) Home: Yakult, Visitor: Hanshin

Hanshin vs. Yakult (Koshien) Home: Hanshin, Visitor: Yakult

The combination of battles in this case is expressed using Python as follows.

#If you have the concept of home, visitor

team = ["Yakult","Hanshin","Yokohama","Chunichi","Hiroshima","Giant"]

for vs_01 in team:
    for vs_02 in team:

        #However, this excludes the same team's match such as Yakult vs. Yakult.
        if vs_01 != vs_02:

            print(vs_01+ " vs " + vs_02)

Yakult vs Hanshin Yakult vs Yokohama Yakult vs Chunichi Yakult vs Hiroshima Yakult vs Giants Hanshin vs Yakult Hanshin vs Yokohama Hanshin vs Chunichi Hanshin vs Hiroshima Hanshin vs Giants Yokohama vs Yakult Yokohama vs Hanshin Yokohama vs Chunichi Yokohama vs Hiroshima Yokohama vs Giants Chunichi vs Yakult Chunichi vs Hanshin Chunichi vs Yokohama Chunichi vs Hiroshima Chunichi vs Giants Hiroshima vs Yakult Hiroshima vs Hanshin Hiroshima vs Yokohama Hiroshima vs Chunichi Hiroshima vs Giants Giants vs Yakult Giants vs Hanshin Giants vs Yokohama Giants vs Chunichi Giants vs Hiroshima

In the case of a combination without the concept of home and visitor

On the other hand, except for the concept of home and visitor as before. If you just want to output the combination of battles between teams, it will be as follows.

#In the case of a combination without the idea of home and visitors

#Prepare two similar lists
team_01 = ["Yakult","Hanshin","Yokohama","Chunichi","Hiroshima","Giant"]
team_02 = ["Yakult","Hanshin","Yokohama","Chunichi","Hiroshima","Giant"]

for vs_01 in team_01:
    
    #Remove duplication from one list from the other list
    team_02.remove(vs_01)

    for vs_02 in team_02:
        print(vs_01+ " vs " + vs_02)

Yakult vs Hanshin Yakult vs Yokohama Yakult vs Chunichi Yakult vs Hiroshima Yakult vs Giants Hanshin vs Yokohama Hanshin vs Chunichi Hanshin vs Hiroshima Hanshin vs Giants Yokohama vs Chunichi Yokohama vs Hiroshima Yokohama vs Giants Chunichi vs Hiroshima Chunichi vs Giants Hiroshima vs Giants

With this, the battle between the same teams such as Yakult vs. Yakult, You can remove the duplication of team battles such as Yakult vs. Hanshin and Hanshin vs. Yakult.

Recommended Posts

[Python] Output battles and combinations (nesting for statements and if statements)
Python basics (variables, types, operators, if statements, for statements)
python input and output
Python Exercise for Beginners # 1 [Basic Data Types / If Statements]
Recursively search for files and directories in Python and output
Useful tricks related to list and for statements in Python
[Code] Module and Python version output
[TouchDesigner] Tips for for statements using python
Notes for Python file input / output
Export and output files in Python
[Super basic] Compare Python, Java and JavaScript (variable, if statement, while statement, for statement)
Python beginner ~ Round off the Nth decimal place and output ~ (for memo)
[Python] I learned about if statements on Udemy, so I'll output them.
difference between statements (statements) and expressions (expressions) in Python
Summary of various for statements in Python
6 Python libraries for faster development and debugging
[Python] Organizing how to use for statements
Python> Output numbers from 1 to 100, 501 to 600> For csv
[python] Difference between rand and randn output
[Python] Various combinations of strings and values
SublimeText2 and SublimeLinter --Syntax check for Python3--
I tried speeding up Python code including if statements with Numba and Cython
Instant method grammar for Python and Ruby (studying)
(Windows) Causes and workarounds for UnicodeEncodeError on Python 3
Read JSON with Python and output as CSV
Output python log to both console and file
[Python for Hikari] Chapter 09-02 Classes (Creating and instantiating classes)
[Python / Chrome] Basic settings and operations for scraping
python memo-"if not A and B" was "if (not A) and B"
PDF files and sites useful for learning Python 3
Generate and output plantuml object diagram from Python object
Install Python and libraries for Python on MacOS Catalina