基于列表中的某些项查找字符串的长度

2024-10-05 12:45:09 发布

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

我有一个项目清单如下

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]也应被视为一个字符,因为这两个字符在词汇表中都作为单独的项目出现


Tags: 项目brstringclshitems字符list
3条回答

在将令牌转义为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))

这里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)

如果没有,您将使用None而不是匹配项

不确定它是否足够像Python,但这里是:

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

因此,我假设您的意思是,您希望查找字符串中也在列表中的字符数。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分的成绩

相关问题 更多 >

    热门问题