# Python, given a word, generate a list with one letter changing for each letter in alphabet

Here is my problem:

Return the number of times that the string code appears anywhere in the given string, except we'll accept any letter for the d, so cope and cooe count.

I solved it by creating a list with all 26 possible co_e combinations and then used two for loops to iterate through the list and search for matches in the given string.

Is there another way I could have created that list? or is there a way to ignore the third character in word and only search for matching characters 1, 2 and 4?

Here is my solution:

if len(inputstring)<4:
return 0
wordlist=['coae','cobe','coce','code'...etc]
count=0

for i in wordlist:
for j in range(len(inputstring)):
if inputstring[j:j+4] == i:
count+=1
return count


## migrated from stackoverflow.comDec 27 '17 at 11:57

This question came from our site for professional and enthusiast programmers.

• @MartijnPieters I realize you did this on the SO question, but just so no one gets the wrong idea on this site: Do not edit a questioner's code with improvements. If you have improvements to suggest, post an answer. – Nic Hartley Dec 27 '17 at 22:55
• @QPaysTaxes yup, it’s relatively common to do so on SO, and I did do before deciding on migration. I’m aware you shouldn’t do so here, feel free to roll it back. – Martijn Pieters Dec 27 '17 at 22:57
• @MartijnPieters I don't see any harm in leaving it; I just don't want people seeing the comment from another site and assuming it's okay here, too. – Nic Hartley Dec 27 '17 at 23:01
• Right, comment nuked. – Martijn Pieters Dec 27 '17 at 23:06

Just use regular expressions!

import re
# creating regular expression with "any" letter at 3 position
r = re.compile("co[a-z]e")
# and taking all the matches:
matches = r.findall("some of my code, nope to come, cofe and cole, or cope and dome, rope and hope!")
count = len(matches)
print(count)

• print(len(matches or [])). – CristiFati Dec 27 '17 at 11:48
• You right! the count was needed! – MihanEntalpo Dec 27 '17 at 11:51
• findall() always returns a list, there is no need to use or here. len(matches) will always work. – Martijn Pieters Dec 27 '17 at 12:00
• It's true, my bad :) – MihanEntalpo Dec 27 '17 at 12:03

You can use in to test if a substring appears:

>>> 'code' in 'recoded'
True


This removes the need to loop over the string.

You can generate your test strings by looping over all letters in the alphabet; Python already has the latter available for you at string.ascii_lowercase and a list comprehension:

import string

possibilities = ['co{l}e'.format(l) for l in string.ascii_lowercase]

count = 0
for possibility in possibilities:
if possibility in inputstring:
count += 1


You could also just test for co appearing, and see if there is a letter e further along. You can use the str.find() method to find the position of an occurrence and search from there; str.find() takes a starting position to search for the next match:

count = 0
start = 0
while True:
position = inputstring.find('co')
if position == -1:
break
if len(inputstring) > position + 2 and inputstring[position + 2] == 'e':
count += 1
start = position + 1


However, most experienced programmers will use regular expressions to find such matches:

import re

count = len(re.findall(r'co[a-z]e', inputstring))


Here the expression uses [a-z] to match a single character as a class, anything in that series (so letters from a to z) would match. The re.findall() function returns a list of all matches found in the input string, so all you have to do then is take the len() length of that list to get a count.

• ['co{l}e'.format(l) for l in string.ascii_lowercase] - possible, regular expression engine make something like this under the hood when you use "co[a-z]e" pattern :) – MihanEntalpo Dec 27 '17 at 11:49
• @MihanEntalpo: a little patience please, I'll get there too! Take into account this user is probably still in the early stages of learning to program, and regular expressions can be quite challenging. – Martijn Pieters Dec 27 '17 at 11:52
• "This removes the need to loop over the string." Strictly speaking, it doesn't remove the loop; you just don't need to do it manually, and there might be some cool optimizations done (for example, if it matches the first three letters of code, then you can skip over those three, since o won't match c) – Nic Hartley Dec 27 '17 at 22:57
• Right, it removes the need to manually loop in Python. The C implementation of the containment search is based on Boyer-Moore; the algorithm does skip in larger steps where possible. – Martijn Pieters Dec 27 '17 at 23:03
• And yes, a generator expression would be slightly more efficient (certainly in terms of memory, less so in terms of speed). I answered with a focus on teaching a beginner however, not with a focus on code review, so I ignored that option for simplicity’s sake. – Martijn Pieters Dec 27 '17 at 23:06

Simple and beautiful. I hope it is what you want

import re

list=['coae','cobe','coce','code','coco']
count=0

for i in list:
if re.match(r'co[a-z]e', i):
count+=1

print(count)