+ 2

Aside from index method, how u can get the unique indexes of duplicate characters in a string or list? (Python)

Ex: "Mississippi" Index of s: 2,3,5,6

4th Apr 2020, 10:26 AM
Yves Manalo
Yves Manalo - avatar
5 Respostas
+ 6
for i in range(len(word)) : if word[i]=="s“: print(i)
4th Apr 2020, 10:30 AM
Oma Falk
Oma Falk - avatar
+ 5
list(filter(word[i] == "s", list(range(len(word))))
4th Apr 2020, 11:21 AM
Oma Falk
Oma Falk - avatar
+ 4
Here is an other try, that is looking for any kind of duplicated characters and digits, and gives an out in a dict: txt = 'Missi2ssi2ppi' ind_tbl = {} for char in sorted(set(txt.lower())): buf = [] if txt.count(char) > 1: for ind, i in enumerate(txt): if i == char: buf.append(ind) ind_tbl.update({char : buf}) print(ind_tbl) # output: {'2': [5, 9], 'i': [1, 4, 8, 12], 'p': [10, 11], 's': [2, 3, 6, 7]}
5th Apr 2020, 8:40 AM
Lothar
Lothar - avatar
+ 3
indexes = lambda string, char: [i for i, x in enumerate(string) if x == char] or None print(indexes('mississipi','s'))
4th Apr 2020, 11:03 AM
visph
visph - avatar
0
Thanks for the tip. Im gonna try to use that to the program im doing rn
4th Apr 2020, 10:37 AM
Yves Manalo
Yves Manalo - avatar