Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test if string ONLY contains given characters

Tags:

python

string

What's the easiest way to check if a string only contains certain specified characters in Python? (Without using RegEx or anything, of course)

Specifically, I have a list of stings, and I want to filter out all of them except the words that are ONLY made up of ANY of the letters in another string. For example, filtering ['aba', 'acba', 'caz'] though 'abc' should give ['aba', 'acba']. (z not in abc)

Just like only keeping the items that can be made using the given letters.

like image 298
Jollywatt Avatar asked Sep 09 '13 09:09

Jollywatt


People also ask

How do I check if a string contains only certain characters?

Use the re. match() method to check if a string contains only certain characters. The re. match method will return a match object if the string only contains the specified characters, otherwise None is returned.

How do you make sure a string only has letters?

In order to check if a String has only Unicode letters in Java, we use the isDigit() and charAt() methods with decision-making statements. The isLetter(int codePoint) method determines whether the specific character (Unicode codePoint) is a letter. It returns a boolean value, either true or false.

How do I check if a string contains only alphabets?

Regex can be used to check a string for alphabets. String. matches() method is used to check whether or not the string matches the given regex.

How do you check if a string contains a set of characters in Python?

Using Python's "in" operator The simplest and fastest way to check whether a string contains a substring or not in Python is the "in" operator . This operator returns true if the string contains the characters, otherwise, it returns false .


3 Answers

Assuming the discrepancy in your example is a typo, then this should work:

my_list = ['aba', 'acba', 'caz']
result = [s for s in my_list if not s.strip('abc')]

results in ['aba', 'acba']. string.strip(characters) will return an empty string if the string to be stripped contains nothing but characters in the input. Order of the characters should not matter.

like image 135
Andrew Gorcester Avatar answered Oct 20 '22 22:10

Andrew Gorcester


You can make use of sets:

>>> l = ['aba', 'acba', 'caz']
>>> s = set('abc')
>>> [item for item in l if not set(item).difference(s)]
['aba', 'acba']
like image 43
alecxe Avatar answered Oct 20 '22 21:10

alecxe


Assuming you only want the strings in your list which have only the characters in your search string, you can easily perform

>>> hay = ['aba', 'acba', 'caz']
>>> needle = set('abc')
>>> [h for h in hay if not set(h) - needle]
['aba', 'acba']

If you wan't to avoid sets, you can also do the same using str.translate. In this case, you are removing all characters which are in your search string.

>>> needle = 'abc'
>>> [h for h in hay if not h.translate(None,needle)]
['aba', 'acba']
like image 6
Abhijit Avatar answered Oct 20 '22 22:10

Abhijit