string - For Loop List Python for Variable URL -


so if wanted print contents of url page or cycle through many , change few segments of url how might so. given following:

if know format following player, minus few tweeks id , last portion: format below:

http://espn.go.com/mlb/player/_/id/31000/brad-brach 

lets know each players id , name:

player_name = ['brad-brach','oliver-drake',...] player_id = ['31000','31615',...] in player_id: url = 'http://espn.go.com/mlb/player/_/id/'+player_id[i]+/'+player_name[i] 

do ever given know these players in player_id , player_name.

how might iterate through player_id's , player_name's without getting

typeerror: list indices must integers, not str

i know url list , contents within player_id[0] string. missing here?

select item list index not string of list, player_name['31000']?!

player_name = ['brad-brach','oliver-drake',...] player_id = ['31000','31615',...] in xrange(len(player_name)):     url = 'http://espn.go.com/mlb/player/_/id/{}/{}'.format(player_id[i], player_name[i]) 

and more elegant solution use zip, @pynchia ;)

player_name = ['brad-brach','oliver-drake',...] player_id = ['31000','31615',...] url_pattern = 'http://espn.go.com/mlb/player/_/id/{}/{}' p_name, p_id in zip(player_name, player_id):     url = url_pattern.format(p_id, p_name) 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -