0

say you have a string, where P = "hello"

how do I split this into individual characters, so I can use indexing.

what I aim to do is replace a letter in P, (e.g h) with a letter in the alpphabet. I want to for loop this so when a new word, e.g hillo with e replaced, is in a seperate list of strings, it will make a list with all these words together. I tried listing, but that doesn't work as I couldn't do the skrt[letter] = alphabet.pop(item).

anagrams = {}
L = []
C = []

alphabet = "abcdefghijklmnopqrstuvwxyz"
alphabet = sorted(alphabet)
print(alphabet)

lines = open("words.txt", "r").readlines()
for word in lines:
    stripped = word.strip().lower()
    L.append(stripped)

for letter in word:
    for item in alphabet:
        skrt = list(word)
        print(skrt)
        skrt[letter] = alphabet.pop(item)
        "".join(skrt)
        if skrt in L:
            C.append(skrt)
            print(C)
swag
  • 39
  • 1
  • 5
  • How about `a = list('abcdef')` or `a = map(None, 'abcdef')` then you can combine it back again using `''.join(a)`. – Eli Sadoff Dec 08 '16 at 19:10
  • if you read my question I tried that but it doesn't work... – swag Dec 08 '16 at 19:12
  • i'm struggling to replace a letter in a with a letter from the alphabet, that is my main problem – swag Dec 08 '16 at 19:12
  • `letter` in your code is an actual *letter*, not an index. Try iterating on a range, like `for letter in len(word)` – Fred Larson Dec 08 '16 at 19:13
  • Let's say you have the string `a = 'hello'` and you want to replace the `'e'` with a `'p'`. You can do this `l = list(a)` then `l[l.index('e')] = 'p'` then `a = ''.join(l)` – Eli Sadoff Dec 08 '16 at 19:14
  • @FredLarson could you correct my code explaining this? I've tried that but it didn't work – swag Dec 08 '16 at 19:22
  • @EliSadoff but I don't want to replace specific letters, I want it to just do it autonomously using my for loop. – swag Dec 08 '16 at 19:23

0 Answers0