+ 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
5 Réponses
+ 6
for i in range(len(word)) :
if word[i]=="s“:
print(i)
+ 5
list(filter(word[i] == "s", list(range(len(word))))
+ 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]}
+ 3
indexes = lambda string, char: [i for i, x in enumerate(string) if x == char] or None
print(indexes('mississipi','s'))
0
Thanks for the tip. Im gonna try to use that to the program im doing rn