我有元组列表。必须根据项目值从列表中排除某些元组。具有2个元组的列表的示例如下:
orig_list = [('mydomain', '20150726', 'e-buyeasy.com', 'www.ujizlhekw.e', '1',
'1', '1', '1', '100.0', '0.0'),
('myotherdomain', '20150726', 'floating-share-buttons.com', '', '26',
'26', '26', '26', '100.0', '0.0')]
我创建了一个函数,它使用两个元组项的正则表达式匹配,而不是其他“查找”。名单。该函数返回True或False,具体取决于与查找列表的匹配。
def tupMatch(tup):
sourceReg="semalt.*","anticrawler.*","best-seo-offer.*","best-seo-solution.*","buttons-for-website.*","buttons-for-your-website.*","7makemoneyonline.*","-musicas*-gratis.*","kambasoft.*","savetubevideo.*","ranksonic.*","medispainstitute.*","offers.bycontext.*","100dollars-seo.*","sitevaluation.*","dailyrank.*","videos-for-your-business.*","videos-for-your-business.*","success-seo.*","4webmasters.*","get-free-traffic-now.*","free-social-buttons.*","trafficmonetizer.*","traffic2money.*","floating-share-buttons.*"
hostnameReg="mydomain.*","myotherdomain.*"
sourceReg2 = "(" + ")|(".join(sourceReg) + ")"
hostnameReg2 = "(" + ")|(".join(hostnameReg) + ")"
sourceMatch = re.match(sourceReg2, tup[2].lower())
hostMatch = re.match(hostnameReg2, tup[3].lower())
if (not sourceMatch) and (hostMatch):
True
else:
False
return
我有一个列表推导,根据功能结果过滤原始列表。
filtered_list = [tup for tup in orig_list if tupMatch(tup)]
但实际上并没有过滤列表。我期待着' tupMatch(tup)'将返回True或False,列表推导将只有True的元组。
我在这里缺少什么?
答案 0 :(得分:2)
以下行不return
(没有return
个关键字);该函数隐式返回None
;这被视为虚假价值。
if (not sourceMatch) and (hostMatch):
True
else:
False
按以下方式更改行:
if (not sourceMatch) and (hostMatch):
return True
else:
return False
或更短时间:
return (not sourceMatch) and (hostMatch) # OR return bool(...)