如何检查字典键值在另一个使用python-2.7的函数中
我正在使用python-2.7,并正在从另一个函数中检索字典的键值 我的代码-如何检查字典键值在另一个使用python-2.7的函数中,python-2.7,dictionary,beautifulsoup,Python 2.7,Dictionary,Beautifulsoup,我正在使用python-2.7,并正在从另一个函数中检索字典的键值 我的代码- function1(self): self.dict1 = {'Old_link':id} # dict1 contains many such key-value wherein key=link,value=id ....#some ....#code function2(self): ....#some ....#code soup = BeautifulSoup(page, "lxml") #create
function1(self):
self.dict1 = {'Old_link':id} # dict1 contains many such key-value wherein key=link,value=id
....#some
....#code
function2(self):
....#some
....#code
soup = BeautifulSoup(page, "lxml") #creates a soup
self.tag1 = soup.findAll("div","class_name")
for i in self.tag1:
self.Old_link = i.find('a').get('href')
try:
#insert into table query
self.id = cursor.lastrowid
self.dict1['Old_link'] = self.id
except:
print(error)
function3(self):
soup = BeautifulSoup(page, "lxml") #creates a soup
self.tag = soup.findAll("div","class_name")
for i in self.tag:
self.New_link = i.find('a').get('href')
if self.New_link == any self.dict1.has_key(self.Old_link):
# get the id and match with other_id
如何将function1()中的新链接与旧链接匹配
我很抱歉问得不对
欢迎提供任何形式的帮助/指导
提前感谢。这些函数看起来像类的方法。您将New_link和Old_link定义为类的属性,因此它们应该可以在类中的任何位置访问。我不确定我是否理解问题所在。你能问一个更具体的问题吗?@wrkyle-我想把dict1中我从function2传递到function1的链接(旧)与function3中的链接(新)匹配起来。另外,旧的链接是dict1中的一个键。请任何人帮忙-这里尽量更具体一点-
code
def function 3():一些代码。。。。。如果New_link==any in(dict1.keys()):打印“True”一些代码。。。。。其他:打印“False”
“code”与上述参考相关