简体   繁体   中英

check if a string contains character except the ones in a given string python

How can I check if a string ( string1 ) contains characters except the ones in the following string ( seq_letters ):

string1 = 'SEQ'

seq_letters = 'ATCGRYMKSWHBVDN'

E and Q are not in seq_letters.

Using set.difference

string1 = 'SEQ'
seq_letters = 'ATCGRYMKSWHBVDN'

print(set(string1).difference(seq_letters))

Output:

{'E', 'Q'}
string1 = 'SEQ'
seq_letters = 'ATCGRYMKSWHBVDN'
result = []
for i in string1:
    if i not in seq_letters:
        result.append(i)
print(result)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM