For example I can have the string 'acagtcas' and I want to find if the string has any characters that aren't a, c, g or t. I've tried using not but I haven't been able to make it work. How can I implement this?
Asked
Active
Viewed 294 times
3 Answers
1
You can use a comprehension to check the validity of each letter, and then use any()
to see whether at least one of them is invalid:
valid_letters = 'acgt'
data = 'acagtcas'
any(letter not in valid_letters for letter in data)
Output:
True

BrokenBenchmark
- 18,126
- 7
- 21
- 33
0
valid_letters = 'acgt'
data = 'acagtcas'
print(bool(set(data)-set(valid_letters)))
output:
True
valid_letters = 'acgt'
data = 'acagtcas'
print(set(data)-set(valid_letters))
Output:
{'s'}

codester_09
- 5,622
- 2
- 5
- 27