我有一个项目清单如下
items_list=[ '$', '^', '#', '(', ')', '-', '.', '/', '1', '2', '3', '4', '5', '6', '7', '=', 'Br',
'C', 'Cl', 'F', 'I', 'N', 'O', 'P', 'S', '[2H]', '[Br-]', '[C@@H]', '[C@@]', '[C@H]', '[C@]',
'[Cl-]', '[H]', '[I-]', '[N+]', '[N-]', '[N@+]', '[N@@+]', '[NH+]', '[NH2+]', '[NH3+]', '[N]',
'[Na+]', '[O-]', '[P+]', '[S+]', '[S-]', '[S@+]', '[S@@+]', '[SH]', '[Si]', '[n+]', '[n-]',
'[nH+]', '[nH]', '[o+]', '[se]', '\\', 'c', 'n', 'o', 's', '!', 'E']
还有我的绳子
string='N[C@H]1C[C@@H](N2Cc3nn4cccnc4c3C2)CC[C@@H]1c1cc(F)c(F)cc1F'
是否有任何pythonic方法根据items_list
中的项目查找此string
的长度
说明:
N
应被视为一个字符,[C@H]
也应被视为一个字符,因为这两个字符在词汇表中都作为单独的项目出现
在将令牌转义为regex之后,可以使用
re.findall
:这里
tokens
将列出:所以长度是44
注意这里的
|
表示“或”限制:请注意,这不会检查令牌是否代表字符串中的所有内容。如果有部分不构成令牌的一部分,那么它们将被忽略。如果要检查字符串实际上是否完全由此类标记组成,则可以检查:
如果没有,您将使用
None
而不是匹配项不确定它是否足够像Python,但这里是:
44
因此,我假设您的意思是,您希望查找字符串中也在列表中的字符数。for循环应执行以下操作:
这给了我44分的成绩
相关问题 更多 >
编程相关推荐