因此,我尝试合并两个表并根据需要获得输出。让我先分享一下表格。
表1:
Teamname threeRPA Value Pos
Washington Wizards (WAS) Points 27.9 Center (C)
San Antonio Spurs (SA) Points 25.4 Center (C)
Charlotte Hornets (CHA) Points 25.3 Center (C)
Philadelphia 76ers (PHI) Points 24.1 Center (C)
Detroit Pistons (DET) Points 23.8 Center (C)
Chicago Bulls (CHI) Points 23.4 Center (C)
Dallas Mavericks (DAL) Points 23.4 Center (C)
Washington Wizards (WAS) Rebounds 18.0 Center (C)
Chicago Bulls (CHI) Rebounds 12.0 Power Forward (PF)
Portland Trail Blazers (POR) Rebounds 11.4 Power Forward (PF)
Oklahoma City Thunder (OKC) Rebounds 11.3 Power Forward (PF)
Washington Wizards (WAS) Rebounds 11.3 Power Forward (PF)
Atlanta Hawks (ATL) Rebounds 11.0 Power Forward (PF)
Denver Nuggets (DEN) Rebounds 10.8 Power Forward (PF)
Charlotte Hornets (CHA) Rebounds 10.8 Power Forward (PF)
table2:
Teamname Player Pos Con
Atlanta Hawks (ATL) Trae Young Point Guard (PG)
Atlanta Hawks (ATL) Dejounte Murray Shooting Guard (SG)
Atlanta Hawks (ATL) Saddiq Bey Small Forward (SF)
Atlanta Hawks (ATL) Jalen Johnson Power Forward (PF)
Atlanta Hawks (ATL) Clint Capela Center (C)
Denver Nuggets (DEN) Jamal Murray Point Guard (PG)
Dallas Mavericks (DAL) Derrick Jones Power Forward (PF)
Denver Nuggets (DEN) Nikola Jokic Center (C)
Atlanta Hawks (ATL) Onyeka Okongwu Center (C)
Washington Wizards (WAS) Kyle Kuzma Power Forward (PF)
So, i am taking two teams as user input suppose was and atl.
The output I want like below
Washington Wizards (WAS) Points 27.9 Center (C) Clint Capela,Onyeka Okongwu
Washington Wizards (WAS) Rebounds 18.0 Center (C) Clint Capela,Onyeka Okongwu
Atlanta Hawks (ATL) Rebounds 11.0 Power Forward (PF) Kyle Kuzma
因此,它将
table1
值与具有该 pos(位置)的反对队球员相匹配
我正在尝试在 python/jupyter 中执行此操作,但没有得到所需的输出。
这是我的代码
import sqlite3
# Connect to the 'NBA.db' database
conn = sqlite3.connect('NBA.db')
cursor = conn.cursor()
# Function to fetch performance data from 'Cheatsheet' based on team name
def fetch_cheatsheet_data(team):
cursor.execute("SELECT * FROM Cheatsheet WHERE teamname LIKE ? OR teamname LIKE ?",
(f'%{team}%', f'({team})%'))
return cursor.fetchall()
# Function to fetch players from 'teamdepth' based on team and position
def fetch_players(team, position):
cursor.execute("SELECT playername FROM teamdepth WHERE teamname LIKE ? AND pos = ?",
(f'%{team}%', position))
return [player[0] for player in cursor.fetchall()]
# User Inputs for Team Names
team1 = input("Enter Team 1 (Full name, Abbreviation, or Both): ").strip().upper()
team2 = input("Enter Team 2 (Full name, Abbreviation, or Both): ").strip().upper()
# Fetch 'Cheatsheet' Data for both teams
team1_data = fetch_cheatsheet_data(team1)
team2_data = fetch_cheatsheet_data(team2)
positions_to_fetch = ['Point Guard (PG)', 'Shooting Guard (SG)', 'Small Forward (SF)', 'Power Forward (PF)', 'Center (C)']
for position in positions_to_fetch:
for data in team1_data:
print(f"{data[0]} \t {data[1]} \t {data[2]} \t {data[3]}")
opposing_team = team2 if data[0].endswith(team1) else team1
players_opposing_team = fetch_players(opposing_team, position)
if players_opposing_team:
print(f"{position} Players for {opposing_team}: {', '.join(players_opposing_team)}")
else:
print(f"No {position} Players found for {opposing_team}")
for data in team2_data:
print(f"{data[0]} \t {data[1]} \t {data[2]} \t {data[3]}")
opposing_team = team1 if data[0].endswith(team2) else team2
players_opposing_team = fetch_players(opposing_team, position)
if players_opposing_team:
print(f"{position} Players for {opposing_team}: {', '.join(players_opposing_team)}")
else:
print(f"No {position} Players found for {opposing_team}")
# Close the connection
conn.close()
这是我的方法:
#!/usr/bin/env python3
# https://stackoverflow.com/q/77770315/459745
import collections
import sqlite3
Cheatsheet = collections.namedtuple("Cheatsheet", "teamname,threerpa,value,pos")
Teamdepth = collections.namedtuple("Teamdepth", "teamname,player,pos")
Output = collections.namedtuple("Output", "teamname,threerpa,value,pos,players")
def custom_row_factory(cursor: sqlite3.Cursor, row: tuple):
field_names = tuple(x[0] for x in cursor.description)
if field_names == Cheatsheet._fields:
return Cheatsheet(*row)
elif field_names == Teamdepth._fields:
return Teamdepth(*row)
elif len(row) == 1:
return row[0]
return row
def show_team(conn: sqlite3.Connection, this_team: str, opposition_team: str):
cheat = conn.execute(
"SELECT * FROM Cheatsheet WHERE teamname LIKE ?",
(f"%{this_team}%",),
)
rows = []
for row in cheat:
players = conn.execute(
"SELECT player FROM Teamdepth WHERE teamname LIKE ?" " AND pos = ?",
(f"%{opposition_team}%", row.pos),
).fetchall()
rows.append(Output(*row, ",".join(players)))
return rows
def main():
"""Entry"""
with sqlite3.connect("NBA.db") as conn:
conn.row_factory = custom_row_factory
fmt = "%-30s %-10s %6.1f %-20s %s"
for row in show_team(conn, "WAS", "ATL"):
print(fmt % row)
for row in show_team(conn, "ATL", "WAS"):
print(fmt % row)
if __name__ == "__main__":
main()
输出:
Washington Wizards (WAS) Points 27.9 Center (C) Clint Capela,Onyeka Okongwu
Washington Wizards (WAS) Rebounds 18.0 Center (C) Clint Capela,Onyeka Okongwu
Washington Wizards (WAS) Rebounds 11.3 Power Forward (PF) Jalen Johnson
Atlanta Hawks (ATL) Rebounds 11.0 Power Forward (PF) Kyle Kuzma
注释
input()
调用custom_row_factory()
以便将行从元组转换为这些命名元组。custom_row_factory()
还将具有1个元素的元组转换为元素本身,从而简化了选择玩家的代码