使用在该函数之外的函数中创建的列表,python
Using a list made in a function outside of that function, python
我在使用在我正在迭代的函数中创建的列表时遇到问题。我目前的代码如下所示:
def get_things(i):
html=str(site[i])
browser = webdriver.Chrome() # Optional argument, if not specified will search path.
browser.get(html);
playerlist=[]
teamlist=[]
all_players = browser.find_elements_by_xpath("//a[@class='name']")
all_teams = browser.find_elements_by_xpath("//td[@class='last']")
for a in all_players:
playerlist.append(str(a.text))
print playerlist
for td in all_teams:
teamlist.append(str(td.text))
print teamlist
browser.quit()
return playerlist, teamlist
然后我想在我的程序后面的另一个函数中使用 teamlist 和 playerlist。
for i in xrange(0,2):
get_things(i)
print teamlist #This is where Im told teamlist doesn't exist
print playerlist #This is where I'm told playerlist doesn't exist
print_sheet(teamlist, playerlist)
我的两个打印语句用于确保程序按应有的方式拾取项目。不过,我的问题是有人告诉我团队列表和播放列表不存在。
NameError: name 'teamlist' is not defined
我相信 return 语句应该使这些对程序的其余部分可用,但事实并非如此。
我怎样才能使这些列表对程序的其余部分可用?
使用以下
(teamlist, playerlist)=get_things(I)
由于您的函数正在返回某些内容,您需要对其进行处理。
示例
def add(a,b):
return a+b
n=add(6,8)
print n #n is 14
您需要将 return 的值从 get_things
分配给一些变量
for i in xrange(0,2):
playerlist , teamlist = get_things(i) # missing this
print teamlist #This is where Im told teamlist doesn't exist
print playerlist #This is where I'm told playerlist doesn't exist
print_sheet(teamlist, playerlist)
我在使用在我正在迭代的函数中创建的列表时遇到问题。我目前的代码如下所示:
def get_things(i):
html=str(site[i])
browser = webdriver.Chrome() # Optional argument, if not specified will search path.
browser.get(html);
playerlist=[]
teamlist=[]
all_players = browser.find_elements_by_xpath("//a[@class='name']")
all_teams = browser.find_elements_by_xpath("//td[@class='last']")
for a in all_players:
playerlist.append(str(a.text))
print playerlist
for td in all_teams:
teamlist.append(str(td.text))
print teamlist
browser.quit()
return playerlist, teamlist
然后我想在我的程序后面的另一个函数中使用 teamlist 和 playerlist。
for i in xrange(0,2):
get_things(i)
print teamlist #This is where Im told teamlist doesn't exist
print playerlist #This is where I'm told playerlist doesn't exist
print_sheet(teamlist, playerlist)
我的两个打印语句用于确保程序按应有的方式拾取项目。不过,我的问题是有人告诉我团队列表和播放列表不存在。
NameError: name 'teamlist' is not defined
我相信 return 语句应该使这些对程序的其余部分可用,但事实并非如此。
我怎样才能使这些列表对程序的其余部分可用?
使用以下
(teamlist, playerlist)=get_things(I)
由于您的函数正在返回某些内容,您需要对其进行处理。
示例
def add(a,b):
return a+b
n=add(6,8)
print n #n is 14
您需要将 return 的值从 get_things
分配给一些变量
for i in xrange(0,2):
playerlist , teamlist = get_things(i) # missing this
print teamlist #This is where Im told teamlist doesn't exist
print playerlist #This is where I'm told playerlist doesn't exist
print_sheet(teamlist, playerlist)