Python 基于列表中的某些项查找字符串的长度
我有一个项目清单如下Python 基于列表中的某些项查找字符串的长度,python,string,nlp,Python,String,Nlp,我有一个项目清单如下 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-]'
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\u列表中的项来查找此字符串的长度
说明:
N应被视为一个字符,因此[C@H]因为这两个标记在词汇表列表中都是单独的项。在将标记转义为regex后,可以使用re.findall:
import re
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'
pattern = '|'.join(re.escape(item) for item in items_list)
tokens = re.findall(pattern, string)
print(len(tokens))
以下是令牌列表:
['N', '[C@H]', '1', 'C', '[C@@H]', '(', 'N', '2', 'C', 'c', '3', 'n', 'n', '4', 'c', 'c', 'c', 'n', 'c', '4', 'c', '3', 'C', '2', ')', 'C', 'C', '[C@@H]', '1', 'c', '1', 'c', 'c', '(', 'F', ')', 'c', '(', 'F', ')', 'c', 'c', '1', 'F']
所以长度是44
注意这里的|表示或
限制:请注意,这不会检查令牌是否代表字符串中的所有内容。如果有部分不构成令牌的一部分,那么它们将被忽略。如果要检查字符串实际上是否完全由此类标记组成,则可以检查:
re.match(f'({pattern})*$', string)
如果没有匹配项,则将没有匹配项。因此,我假设您的意思是要查找列表中字符串中的字符数。for循环应执行以下操作:
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'];
length = 0;
string = 'N[C@H]1C[C@@H](N2Cc3nn4cccnc4c3C2)CC[C@@H]1c1cc(F)c(F)cc1F';
count = 0;
for i in string: # Annoyingly, Python only has foreach statements...
if (string[count] in items_list): # If this letter is in your list:
length += 1; # Length is one more
if (count > len(items_list) + 1): # If we have two letters to work with:
if ((string[count] + string[count + 1]) in items_list): # If the next two letters added together is an item in your list:
length += 1; # Length is one more
count += 1; # Skip the next two letters
if (count > len(items_list) + 2): # Same as above for three letters:
if ((string[count] + string[count + 1] + string[count + 2]) in items_list):
length += 1;
count += 2;
if (count > len(items_list) + 3): # Same as above but for four letters:
if ((string[count] + string[count + 1] + string[count + 2] + string[count + 3]) in items_list):
length += 1;
count += 3;
if (count > len(items_list) + 4): # And five:
if ((string[count] + string[count + 1] + string[count + 2] + string[count + 3] + string[count + 4]) in items_list):
length += 1;
count += 3;
count+= 1;
print(length);
这给了我一个44的结果。不确定它是否足够像蟒蛇,但是,这里是:
symbols = set(items_list)
size=0
start=0
while start<len(string):
end=start+1
while end<=len(string):
if string[start:end] in symbols:
print(string[start:end])
size+=1
start=end-1
break
end+=1
start+=1
print(size)
44您的预期输出是什么?您所说的字符串长度是什么意思?最短的可能?最长的?任何组合?假设列表中的项目为字符,然后根据列表中的项目查找此字符串的长度。如果我理解正确,您的意思是N是单个字符,类似地[C@H]是的,这是正确的。如果投票人想解释问题,这将是有益的,谢谢。最近,很多人对regex相关问题的回答都遭到了沉默的反对票,这让人们对这些问题的关注有点神秘。对我来说似乎很好。