我目前正在学习Python,我正在尝试破译我在网上找到的代码。代码的要点是将原始字符串与用户输入键进行比较,如果匹配,则返回原始字符串。你知道吗
我很难理解re.findall()
在这段代码中做了什么
所以头[0]包含一个数据字符串
('2016-12-22 06:28:36', u'Kith x New Era K 59FIFTY Cap - Pink', u'http://kithnyc.com/products/kith-x-new-era-59fifty-cap-pink')
键包含原始字符串
key=r'Nike|Ultra'
head = self.data
for k in key:
print k
flag=re.findall(k,str(head[0]),flags=re.I)
print len(flag)
if len(flag)>4:
print head[0]
据我所知,代码的目的是循环遍历键,看看它是否与head[0]匹配。如果匹配,则返回head[0]。但是,它仍在返回,head[0]
('2016-12-22 06:28:36', u'Kith x New Era K 59FIFTY Cap - Pink', u'http://kithnyc.com/products/kith-x-new-era-59fifty-cap-pink')
即使不匹配。你知道吗
使用following code然后:
输出:
nike item
和ultra item
。你知道吗在代码中,使用
for k in key:
遍历模式的字符。使用re.findall
,在k
中搜索与单个字符匹配的所有非重叠匹配项,只检查head[0]
,不考虑所有其他项。你知道吗相关问题 更多 >
编程相关推荐