简体   繁体   English

检查字符串是否包含列表以外的字符串

[英]Check if a string contains a string except a list

I have a string as follows:我有一个字符串如下:

f = 'ATCTGTCGTYCACGT'

I want to check whether the string contains any characters except: A , C , G or T , and if so, print them.我想检查字符串是否包含除ACGT之外的任何字符,如果是,则打印它们。

for i in f:                                                                                                                        
    if i != 'A' and i != 'C' and i != 'G' and i != 'T':                                                                             
        print(i)

Is there a way to achieve this without looping through the string?有没有办法在不循环遍历字符串的情况下实现这一点?

Depending on the size of your input string, the for loop might be the most efficient solution.根据输入字符串的大小, for循环可能是最有效的解决方案。

However, since you explicitly ask for a solution without an explicit loop, this can be done with a regex.但是,由于您明确要求没有显式循环的解决方案,因此可以使用正则表达式来完成。

import re

f = 'ABCDEFG'

print(*re.findall('[^ABC]', f), sep='\n')

Outputs产出

D
E
F
G

You can use set to achieve the desired output.您可以使用set来实现所需的输出。

f = 'ATCTGTCGTYCACGTXYZ'
not_valid={'A', 'C', 'G' , 'T'}
unique=set(f)
print(unique-not_valid)

output输出

{'Y','X','Z'} #characters in f which are not equal to 'A','C','G','T'

Just do做就是了

l = ['A', 'C', 'G', 'T']

for i in f:
    if i not in l:
        print(i)

It checks whether the list contains a char of the list它检查列表是否包含列表的字符


If you don't want to loop through the list you can do:如果您不想遍历列表,您可以执行以下操作:

import re

l = ['A', 'C', 'G', 'T']

contains = bool(re.search("%s" % "[" + "".join(l) + "]", f))

Technically this loops but we convert your input string to a set which removes duplicate values从技术上讲,这是循环,但我们将您的输入字符串转换为一个删除重复值的集合

accepted_values = ['a','t','c','g']

input = 'ATCTGTCGTYCACGT'

print([i for i in set(input.lower()) if i not in accepted_values])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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