4

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?

BrokenBenchmark
  • 18,126
  • 7
  • 21
  • 33
Miguens
  • 49
  • 1

3 Answers3

3

You can use set.difference:

s = "acagtcas"

x = set(s).difference("acgt")
print(x)

Prints:

{'s'}
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
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