Home > other >  Remove specific character from array and create multiple ones from initial array
Remove specific character from array and create multiple ones from initial array

Time:09-22

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)

CodePudding user response:

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.

  • Related