Using for statement nesting and if statements in Python I tried to output the combination of professional baseball games.
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
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