How to use a list of numbers as index inputs

So I have a list of numbers (answer_index) which correlate to the index locations (indicies) of a characters (char) in a word (word). I would like to use the numbers in the list as index inputs later (indexes) on in code to replace every character except my chosen character(char) with "*" so that the final print (new_word) in this instance would be (****ee) instead of (coffee). it is important that (word) maintains it's original value while (new_word) becomes the modified version. Does anyone have a solution for turning a list into valid index inputs? I will also except easier ways to meet my goal. (Note: I am extremely new to python so I'm sure my code looks horrendous) Code below:

word = 'coffee' 
print(word)
def find(string, char):
  for i, c in enumerate(string):
        if c == char:
            yield i

string = word
char = "e"
indices = (list(find(string, char)))
answer_index = (list(indices))
print(answer_index)
for t in range(0, len(answer_index)):
 answer_index[t] = int(answer_index[t])
indexes = [(answer_index)] 
new_character = '*' 
result = '' 
for i in indexes:
    new_word = word[:i] + new_character + word[i+1:] 
print(new_word)

Upvotes: 0

Views: 60

Answers (3)

Marat
Marat

Reputation: 15738

The easiest and fastest way to replace all characters except some is to use regular expression substitution. In this case, it would look something like:

import re
re.sub('[^e]', '*', 'coffee')  # returns '****ee'

Here, [^...] is a pattern for negative character match. '[^e]' will match (and then replace) anything except "e".

Other options include decomposing the string into an iterable of characters (@PaulM's answer) or working with bytearray instead

Upvotes: 0

Duarte P
Duarte P

Reputation: 154

In Python, it's often not idiomatic to use indexes, unless you really want to do something with them. I'd avoid them for this problem and instead just iterate over the word, read each character and and create a new word:

word = "coffee"
char_to_keep = "e"

new_word = ""
for char in word:
    if char == char_to_keep:
        new_word += char_to_keep
    else:
        new_word += "*"

print(new_word)
# prints: ****ee

Upvotes: 0

Paul M.
Paul M.

Reputation: 10809

You hardly ever need to work with indices directly:

string = "coffee"
char_to_reveal = "e"

censored_string = "".join(char if char == char_to_reveal else "*" for char in string)
print(censored_string)

Output:

****ee

If you're trying to implement a game of hangman, you might be better off using a dictionary which maps characters to other characters:

string = "coffee"
map_to = "*" * len(string)

mapping = str.maketrans(string, map_to)

translated_string = string.translate(mapping)
print(f"All letters are currently hidden: {translated_string}")


char_to_reveal = "e"
del mapping[ord(char_to_reveal)]


translated_string = string.translate(mapping)
print(f"'{char_to_reveal}' has been revealed: {translated_string}")

Output:

All letters are currently hidden: ******
'e' has been revealed: ****ee

Upvotes: 2

Related Questions