I'm trying to randomly insert characters into a string and I want to be able to strip them out later, so I have to use characters that are not already in the string. I want to use as many characters as possible. How can I get a list of all the characters that are not in the string? I am using Python 2.
Assuming you have a set of all possible characters:
>>> characters = set('ABCabc')
then it is as simple as
>>> my_str = "abbaAC"
>>> not_in_string = characters - set(my_str)
>>> not_in_string
set(['c', 'B'])
The big assumption I'm making here is that you're working with an ASCII string.
Valid characters have integer values between 0 and 255. As such, the following will generate a complete set of all of the valid characters:
all_chars = set(chr(i) for i in range(256))
You can then get the set of characters in your string. That's as easy as running set(mystring).
The difference between those is the subset of what's in all_chars, but not in your string:
unused_chars = all_chars - set(mystring)
So putting that all together:
def get_unused_chars(mystring):
# Generate the list of every valid ASCII character
all_chars = set(chr(i) for i in range(256))
# Determine which characters are unused
unused_chars = all_chars - set(mystring)
return unused_chars
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With