简体   繁体   中英

How can I print double letters from string in a list without using regular expressions

I would like to know how to find the appearance of double letters in a list of strings without using regular expressions. Below is what I have so far.

word="kookss"
new_words=["laal","mkki"]

def double_letter(word):
       for i in range(len(word)-1):
            if word[i]== word[i+1]:
                return (word[i],word[i+1])
 print(double_letter(word))
 for w in range(len(new_words)-1):
      print(double_letter(new_words))

output :
 ["OO","ss"]
 ["aa"]
 ["kk"]
word="kookss"
new_words=["laal","mkki"]

def double_letter(word):
    # each double letter found should be put in this list.
    double_letters = []
    for i in range(len(word)-1):
        if word[i]== word[i+1]:
            double_letters.append(word[i] + word[i+1])
    return double_letters

print(double_letter(word))

for w in new_words:
    # for each word `w` in list `new_words` call double_letter method
    print(double_letter(w))

output:

['oo', 'ss']
['aa']
['kk']

your code is not working because:

for w in range(len(new_words)-1):
      print(double_letter(new_words))

This code you are passing new_words (which is a list) to double_letter method which is expecting a single word. Suddenly word[i]== word[i+1] becomes "laal" == "mkki" which is false so your return is None

you pass the same list twice so you get 2 None .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM