我的目的是让程序列出文本文件中包含3组双字母的所有字符串。如果找到3个或更多双字母集,则该函数应返回True:
def three_double(s):
doubnum = 0
i=0
while i < len(s)-1:
if s[i] == s[i+1]:
doubnum += 1
elif doubnum >= 3:
return True
else:
i += 1
return False
我不确定为什么它不打印任何东西。这是该计划的其余部分。
# Function to read and apply the three_double test to each string in
# an input file. It counts the number of results.
def find_three_double(fin):
count = 0
for w in fin:
w = w.strip()
if three_double(w):
print w
count = count + 1
if count == 0:
print '<None found>'
else:
print count, 'found'
# Bring in a package to access files over the web
import urllib
# Access the file containing the valid letters
words_url = "http://thinkpython.com/code/words.txt"
words_file = urllib.urlopen(words_url)
# Apply the actual test
find_three_double(words_file)
答案 0 :(得分:2)
我最初没有仔细阅读您的代码,结果发现它与read()
或readlines()
无关,因为您正在find_three_doubles()
函数中进行迭代。
在three_double()
函数中:
while i < len(s)-1:
if s[i] == s[i+1]:
doubnum += 1
elif doubnum >= 3:
return True
else:
i += 1
return False
有两个问题:
i
增加1,否则如果存在“双重”,则while循环将永远不会停止。elif
更改为if
,否则将无法选择一些合格的字词。固定代码:
def three_double(s):
doubnum = 0
i=0
while i < len(s)-1:
if s[i] == s[i+1]:
doubnum += 1
if doubnum >= 3:
return True
i += 1
return False
# Function to read and apply the three_double test to each string in
# an input file. It counts the number of results.
def find_three_double(fin):
count = 0
for w in fin:
w = w.strip()
if three_double(w):
print w
count = count + 1
if count == 0:
print '<None found>'
else:
print count, 'found'
# Bring in a package to access files over the web
import urllib
# Access the file containing the valid letters
words_url = "http://thinkpython.com/code/words.txt"
words_file = urllib.urlopen(words_url)
# Apply the actual test
find_three_double(words_file)
结果:
aggressiveness
aggressivenesses
allottee
allottees
appellee
appellees
barrenness
barrennesses
bookkeeper
bookkeepers
bookkeeping
bookkeepings
cheerlessness
cheerlessnesses
committee
committees
greenness
greennesses
heedlessness
heedlessnesses
heelless
hyperaggressiveness
hyperaggressivenesses
keelless
keenness
keennesses
masslessness
masslessnesses
possessiveness
possessivenesses
rottenness
rottennesses
sleeplessness
stubbornness
stubbornnesses
successfully
suddenness
suddennesses
sullenness
sullennesses
toolless
wheelless
whippoorwill
whippoorwills
woodenness
woodennesses
46 found
答案 1 :(得分:1)
itertools.groupby
可以大大简化您的程序(=更少的错误)
from itertools import groupby
import urllib
def find_three_double(words_file):
for word in words_file:
word = word.strip()
if sum(sum(1 for i in g) == 2 for k,g in groupby(word)) == 3:
print word
# Access the file containing the valid letters
words_url = "http://thinkpython.com/code/words.txt"
words_file = urllib.urlopen(words_url)
# Apply the actual test
find_three_double(words_file)
说明:
在生成器表达式中我们看到groupby(word)
。这会扫描单词并将双字母聚集在一起。
sum(1 for i in g)
适用于每个群组。它相当于找到组的长度。如果长度为2,那么这是一个双字母,因此sum(1 for i in g) == 2
评估为True
外部sum()
将所有True
和False
值相加,True
添加为1
,False
添加为{ {1}}。如果正好有3个0
值,则会打印单词
答案 2 :(得分:0)
while i < len(s)-1:
if s[i] == s[i+1]:
doubnum += 1
elif doubnum >= 3:
return True
else:
i += 1
如果您的首次检查(s[i] == s[i+1]
)为True
,则您永远不会增加i
,以便循环继续。