Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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’]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading