Python关于芬德尔()本合同目的

2024-10-03 13:23:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在学习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')

即使不匹配。你知道吗


Tags: 字符串代码recomhttpnewheadflag
1条回答
网友
1楼 · 发布于 2024-10-03 13:23:32

It is suppose to print items in head if it match key regex.

使用following code然后:

import re
head = ('2016-12-22 06:28:36', 'nike item', 'ultra item', 'Kith x New Era K 59FIFTY Cap - Pink', 'http://kithnyc.com/products/kith-x-new-era-59fifty-cap-pink')
key=r'Nike|Ultra'  # This is a regex pattern, matches `Nike` or `Ultra` 
for s in head:     # Iterate the items in head
    if re.search(key, s, flags=re.I): # Search for a match in each item, case insensitively
        print(s)  # Print if found

输出:nike itemultra item。你知道吗

在代码中,使用for k in key:遍历模式的字符。使用re.findall,在k中搜索与单个字符匹配的所有非重叠匹配项,只检查head[0],不考虑所有其他项。你知道吗

相关问题 更多 >