首页 > 解决方案 > 如何跨多个子列表获取同一索引处的值?

问题描述

我有一个功能可以访问网站并抓取一些数据并返回包含该数据的列表列表。我需要帮助的是从子列表中获取正确的值并将其映射到 PlayerStats 对象。基本上,列表中 index[0] 处的所有数据都是相关的并且属于同一个玩家。与 index[1] 等相同。

public List<List<string>> GetPlayerStatsFromNbaReference()
    {
        HtmlWeb web = new HtmlWeb();
        HtmlDocument doc = web.Load("https://www.basketball-reference.com/leagues/NBA_2020_per_game.html");

        List<string> names = new List<string>();
        List<string> positions = new List<string>();
        List<string> teams = new List<string>();
        List<string> minutes = new List<string>();
        List<string> fieldGoalAttempts = new List<string>();
        List<string> fieldGoalMakes = new List<string>();
        List<string> threePointAttempts = new List<string>();
        List<string> threepointMakes = new List<string>();
        List<string> freeThrowAttempts = new List<string>();
        List<string> freeThrowMakes = new List<string>();
        List<string> freeThrowPercentage = new List<string>();
        List<string> rebounds = new List<string>();
        List<string> offensiveRebounds = new List<string>();
        List<string> defensiveRebounds = new List<string>();
        List<string> assists = new List<string>();
        List<string> steals = new List<string>();
        List<string> blocks = new List<string>();
        List<string> points = new List<string>();

        var playerName = doc.DocumentNode.SelectNodes("//td[@data-stat='player']/a");
        var playerPosition = doc.DocumentNode.SelectNodes("//td[@data-stat='pos']");
        var playerTeam = doc.DocumentNode.SelectNodes("//td[@data-stat='team_id']/a");
        var playerMinutesPerGame = doc.DocumentNode.SelectNodes("//td[@data-stat='mp_per_g']");
        var playerFieldGoalAttemptsPerGame = doc.DocumentNode.SelectNodes("//td[@data-stat='fga_per_g']");
        var playerFieldgoalMakesPerGame = doc.DocumentNode.SelectNodes("//td[@data-stat='fg_per_g']");
        var playerThreePointAttemptsPerGame = doc.DocumentNode.SelectNodes("//td[@data-stat='fg3a_per_g']");
        var playerThreePointMakesPerGame = doc.DocumentNode.SelectNodes("//td[@data-stat='fg3_per_g']");
        var playerFreeThrowAttemptsPerGame = doc.DocumentNode.SelectNodes("//td[@data-stat='fta_per_g']");
        var playerFreeThrowMakesPerGame = doc.DocumentNode.SelectNodes("//td[@data-stat='ft_per_g']");
        var playerFreeThrowPercentage = doc.DocumentNode.SelectNodes("//td[@data-stat='ft_pct']");
        var playerReboundsPerGame = doc.DocumentNode.SelectNodes("//td[@data-stat='trb_per_g']");
        var playerOffenseiveReboundsPerGame = doc.DocumentNode.SelectNodes("//td[@data-stat='orb_per_g']");
        var playerDefensiveReboundsPerGame = doc.DocumentNode.SelectNodes("//td[@data-stat='drb_per_g']");
        var playerAssistsPerGame = doc.DocumentNode.SelectNodes("//td[@data-stat='ast_per_g']");
        var playerStealsPerGame = doc.DocumentNode.SelectNodes("//td[@data-stat='stl_per_g']");
        var playerBlocksPerGame = doc.DocumentNode.SelectNodes("//td[@data-stat='blk_per_g']");
        var playerPointsPerGame = doc.DocumentNode.SelectNodes("//td[@data-stat='pts_per_g']");

        foreach(var name in playerName)
        {
            names.Add(name.InnerText);
        }

        foreach(var pos in playerPosition)
        {
            positions.Add(pos.InnerText);
        }

        foreach (var team in playerTeam)
        {
            teams.Add(team.InnerText);
        }

        foreach(var min in playerMinutesPerGame)
        {
            minutes.Add(min.InnerText);
        }

        List<List<string>> playerStats = new List<List<string>>()
        {
            names,
            positions,
            teams,
            minutes,
            fieldGoalAttempts,
            fieldGoalMakes,
            threePointAttempts,
            threepointMakes,
            freeThrowAttempts,
            freeThrowMakes,
            freeThrowPercentage,
            rebounds,
            offensiveRebounds,
            defensiveRebounds,
            assists,
            steals,
            blocks,
            points
        };


        return playerStats;
    }
}


public class PlayerStats
{
    public string Name { get; set; }
    public string Position { get; set; }
    public string Team { get; set;}
    public decimal MinutesPerGame { get; set; }
    public decimal FieldGoalAttemptsPerGame { get; set; }
    public decimal FieldgoalMakesPerGame { get; set; }
    public decimal ThreePointAttemptsPerGame { get; set; }
    public decimal ThreePointMakesPerGame { get; set; }
    public decimal ThrowAttemptsPerGame { get; set; }
    public decimal ThrowMakesPerGame { get; set; }
    public decimal FreeThrowPercentage { get; set; }
    public decimal ReboundsPerGame { get; set; }
    public decimal OffenseiveReboundsPerGame { get; set; }
    public decimal DefensiveReboundsPerGame { get; set; }
    public decimal AssistsPerGame { get; set; }
    public decimal StealsPerGame { get; set; }
    public decimal BlocksPerGame { get; set; }
    public decimal PointsPerGame { get; set; }
}

标签: c#

解决方案


这可能不是最好的方法,但这就是我解决问题的方法。

var output = (names.Zip(teams, (first, second) => new { Name = first, Team = second })
                           .Zip(positions, (first, second) => new { Name = first.Name, Team = first.Team, Position = second })
                           .Zip(minutes, (first, second) => new { Name = first.Name, Team = first.Team, Position = first.Position, Minutes = second })
                           .Zip(fieldGoalAttempts, (first, second) => new { Name = first.Name, Team = first.Team, Position = first.Position, Minutes = first.Minutes, FGA = second })
                           .Zip(fieldGoalMakes, (first, second) => new { Name = first.Name, Team = first.Team, Position = first.Position, Minutes = first.Minutes, FGA = first.FGA, FGM = second })
                           .Zip(threePointAttempts, (first, second) => new { Name = first.Name, Team = first.Team, Position = first.Position, Minutes = first.Minutes, FGA = first.FGA, FGM = first.FGM, TPA = second })
                           .Zip(threepointMakes, (first, second) => new { Name = first.Name, Team = first.Team, Position = first.Position, Minutes = first.Minutes, FGA = first.FGA, FGM = first.FGM, TPA = first.TPA, TPM = second })
                           .Zip(freeThrowAttempts, (first, second) => new { Name = first.Name, Team = first.Team, Position = first.Position, Minutes = first.Minutes, FGA = first.FGA, FGM = first.FGM, TPA = first.TPA, TPM = first.TPM, FTA = second })
                           .Zip(freeThrowMakes, (first, second) => new { Name = first.Name, Team = first.Team, Position = first.Position, Minutes = first.Minutes, FGA = first.FGA, FGM = first.FGM, TPA = first.TPA, TPM = first.TPM, FTA = first.FTA, FTM = second })
                           .Zip(freeThrowPercentage, (first, second) => new { Name = first.Name, Team = first.Team, Position = first.Position, Minutes = first.Minutes, FGA = first.FGA, FGM = first.FGM, TPA = first.TPA, TPM = first.TPM, FTA = first.FTA, FTM = second })
                           .Zip(rebounds, (first, second) => new { Name = first.Name, Team = first.Team, Position = first.Position, Minutes = first.Minutes, FGA = first.FGA, FGM = first.FGM, TPA = first.TPA, TPM = first.TPM, FTA = first.FTA, FTM = first.FTM, Rebounds = second})
                           .Zip(defensiveRebounds, (first, second) => new { Name = first.Name, Team = first.Team, Position = first.Position, Minutes = first.Minutes, FGA = first.FGA, FGM = first.FGM, TPA = first.TPA, TPM = first.TPM, FTA = first.FTA, FTM = first.FTM, Rebounds = first.Rebounds, Dreb = second })
                           .Zip(offensiveRebounds, (first, second) => new { Name = first.Name, Team = first.Team, Position = first.Position, Minutes = first.Minutes, FGA = first.FGA, FGM = first.FGM, TPA = first.TPA, TPM = first.TPM, FTA = first.FTA, FTM = first.FTM, Rebounds = first.Rebounds, Dreb = first.Dreb, Oreb = second })
                           .Zip(assists, (first, second) => new { Name = first.Name, Team = first.Team, Position = first.Position, Minutes = first.Minutes, FGA = first.FGA, FGM = first.FGM, TPA = first.TPA, TPM = first.TPM, FTA = first.FTA, FTM = first.FTM, Rebounds = first.Rebounds, Dreb = first.Dreb, Oreb = first.Oreb, Assists = second })
                           .Zip(steals, (first, second) => new { Name = first.Name, Team = first.Team, Position = first.Position, Minutes = first.Minutes, FGA = first.FGA, FGM = first.FGM, TPA = first.TPA, TPM = first.TPM, FTA = first.FTA, FTM = first.FTM, Rebounds = first.Rebounds, Dreb = first.Dreb, Oreb = first.Oreb, Assists = first.Assists, Steals = second })
                           .Zip(blocks, (first, second) => new { Name = first.Name, Team = first.Team, Position = first.Position, Minutes = first.Minutes, FGA = first.FGA, FGM = first.FGM, TPA = first.TPA, TPM = first.TPM, FTA = first.FTA, FTM = first.FTM, Rebounds = first.Rebounds, Dreb = first.Dreb, Oreb = first.Oreb, Assists = first.Assists, Steals = first.Steals, Blocks = second })
                           .Zip(points, (first, second) => new { Name = first.Name, Team = first.Team, Position = first.Position, Minu

推荐阅读