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.
5 Answers
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
2 Comments
The following should do the trick (given that you want your output string to have a specific size):
from string import letters
from random import choice
def random_letters(not_in, size=1):
i = 1
out = []
while(i <= size):
char = choice(letters)
if char.lower() not in not_in:
out.append(char)
i += 1
return ''.join(c for c in out)
print(random_letters('abc', 3))
this outputs a random string without a, b or c of size 3.
sets. (e.g. :set('some letters') - set('other letters'))string.ascii_lettersandset.