Home > Software engineering >  Python: How to get the 2nd and 4th element of a list, then every 4th element of each after?
Python: How to get the 2nd and 4th element of a list, then every 4th element of each after?

Time:10-01

I have a web-scraped list that contains each players number, name, hometown, and position in one continuous list.

my_list = [1, Bob, Austin, Pitcher, 2, Jim, New York, Catcher, 3, Will, Kansas, Left Field, ...]

I need to be able to access just the name and position of each player in the list.

Ex: result_list = [Bob, Pitcher, Jim, Catcher, Will, Left Field]

I have tried using an if statement with a count nested in a for loop for each element in 'my_list' but I can't seem to get it.

Here is the full code, the only difference is I am also using a for loop for each team in the conference in order to get the right 'team' variable for my web scraping and I need the 2nd element, the 6th element, and each 7th element of each after that (my end goal is to add each players name and position to a NEW player model):

def league(request, league_id, league_title):

south_conf = ['Chemeketa','Clackamas','Clark','Lane','Linn-Benton','Mt Hood','SW Oregon','Umpqua'] USER_AGENT = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36" LANGUAGE = "en-US,en;q=0.5" session = requests.Session() session.headers['User-Agent'] = USER_AGENT session.headers['Accept-Language'] = LANGUAGE session.headers['Content-Language'] = LANGUAGE

for team in south_conf:
    team = str(team)
    team = team.lower()
    team = team.replace(' ','')
    team = team.replace('-','')
    team = team.replace('sworegon', 'southwesternoregon')
    team_html_content = session.get(f'https://nwacsports.com/sports/bsb/2021-22/teams/{team}?view=roster').text
    team_soup = BeautifulSoup(team_html_content, 'html.parser')
    team_rows = team_soup.find_all('table')
    team_rows = team_rows[3]
    count = 0
    for ele in team_rows.find_all('td')[1::]:
        if count % 7 == 0:
            obj = Player()
            obj.name = str(ele.text)
            obj.team = team
            obj.save()
            print(ele.text)
            
            count  = 1
        else:
            count  = 1


return render(request, 'league/league.html', context)

CodePudding user response:

Use

my_list[1::2]

where the 1 means to start at index 1 (which is Bob) and the 2 gives every second element after that (Pitcher, Jim, etc.)

EDIT:

I do not understand what it is you want to do. But I suggest you work with a numpy array (which makes slicing somewhat easier):

my_array = np.array(my_list).reshape(3, 4)

Now you can get all information from player i by doing my_array[i,:]. Or get all names by doing my_array[:,1].

Another option is to use pandas to create a dataframe:

df = pd.DataFrame(data=np.array(my_list).reshape(3, 4), columns=['nr', 'Name', 'City', 'Position'])

Now you can get all information from player i by doing df.iloc[i]. Or get all names by doing df.Name.

I hope this provides enough information to continue your work :D

  • Related