Reputation: 31
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.
Upvotes: 2
Views: 1496
Reputation: 9839
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
Upvotes: 1
Reputation: 77454
What is a "letter"?
Assuming ascii:
set(string.ascii_letters) - set(yourstring)
otherwise, define your alphabet appropriately first, then use
set(youralphabet) - set(yourstring)
Upvotes: 2
Reputation: 652
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.
Upvotes: 0
Reputation: 5685
You can use a list comprehension to achieve this:
>>> not_in = 'abcde'
>>> other_string = 'remove char in'
>>> char_not_in = "".join([c for c in other_string if not c in not_in])
>>> print(char_not_in)
'rmov hr in'
Upvotes: 0
Reputation: 530970
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'])
Upvotes: 5