Remove specific character from array and create multiple ones from initial array

Question:

I have an input as: [a, b, c, *]

If my input has * then I need to create 29 different versions (29 letters in Turkish alphabet) of the input list like below. I also need to remove * from new arrays:

harfler[1]: [‘a’, ‘b’, ‘c’, ‘a’]
harfler[2]: [‘a’, ‘b’, ‘c’, ‘b’]
.
.
.
harfler[29]: [‘a’, ‘b’, ‘c’, ‘z’]

I tried to use for loop but didn’t get what I want. How can I achieve this?

letters=input("Enter the letters")

alphabet=['a','b','c']

letters_array=[]
letters=letters.join(alphabet)

for character in alphabet:
  letters=letters.join(alphabet)
  letters_array.append(letters)

print(letters_array)
Asked By: Yusuf Ziya Güleç

||

Answers:

I think what you want is something like this:

dummy = list('abc*')
alphabet = ['a', 'b', 'c']

if '*' in dummy:
    starIdx = dummy.index('*')
    allVersions = []
    for c in alphabet:
        newVersion = dummy.copy()
        newVersion[starIdx] = c
        allVersions.append(newVersion)
    print(allVersions)

This would output:

[['a', 'b', 'c', 'a'], ['a', 'b', 'c', 'b'], ['a', 'b', 'c', 'c']]

Mind you, that is only for one star in your input. More stars, and you run into combinations of versions.

Answered By: Nyquist
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.