2017-08-13 48 views
-1

我是python的新手,我正在解决一个问题。谁能告诉应有之义,如果下面的代码行:Python正则表达式理解

if collections.Counter(re.findall(r"[\w']+", decrypted))[repeat] >= 2: 
      return decrypted 

解密是一个长字符串,然后重复是在串词。

预先感谢您。

回答

0

让我们:

decrypted = "foo bar baz" 

然后

re.findall(r"[\w']+", decrypted) 

回报listdecrypted子的匹配规则r"[\w']+"所有字母,数字或单引号符号字符串。结果是['foo', 'bar', 'baz']

方法collections.Counter创建一个特殊的dict像列表中的对象。此对象的运算符[x]返回给定列表中的x的计数。

最后:

collections.Counter(re.findall(r"[\w']+", decrypted))[repeat] 

回报decryptedrepeat亚系。

0

\w任何单词字符(相当于[a-zA-Z0-9_]意思所有的字母和数字,下划线

+意味着1次以上(但至少1)

您尝试findall(全球)匹配

online explaination