Stupid420
Stupid420

Reputation: 1419

Finding length of a string based on certain items in a list

I have an items list as follows.

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']

And my string as as under.

string='N[C@H]1C[C@@H](N2Cc3nn4cccnc4c3C2)CC[C@@H]1c1cc(F)c(F)cc1F'

Is there any pythonic way to find the length of this string based on the items in items_list?

Explanation:

N should be taken as one character and so is [C@H] because both of these are present as separate items in the list of vocabulary.

Upvotes: 2

Views: 73

Answers (3)

Thiago Curvelo
Thiago Curvelo

Reputation: 3740

Not sure if it is pythonic enough but, here it goes:

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

Upvotes: 2

kettle
kettle

Reputation: 460

So, I'm assuming you mean that you want to find the number of characters in the string that are also in your list. A for loop should do this:

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);

This gave me a result of 44.

Upvotes: 0

alani
alani

Reputation: 13079

You can use re.findall after escaping the tokens as regex:

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

Here tokens will be the list:

['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']

so the length is 44.

Note that the | here means "or".

Limitation: note that this does not check that the tokens account for everything in the string. If there are parts that do not form part of a token, then they will simply be ignored. If you want to check that the string in fact consists entirely of such tokens, then you can check:

re.match(f'({pattern})*$', string)

In the event that it does not you will have None instead of a match.

Upvotes: 2

Related Questions