0

我的意图是让程序列出文本文件中包含 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)
4

3 回答 3

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

有两个问题:

  • 您需要增加i1,否则如果有“双”,while 循环将永远不会停止。
  • 您还需要更改elifif此处,否则将无法选择某些合格的单词。

固定代码:

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
于 2012-10-23T01:26:29.337 回答
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()将所有TrueFalse值相加,True加为1False加为0。如果恰好有 3 个True值,则打印该单词

于 2012-10-23T03:31:49.313 回答
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,因此循环将永远继续。

于 2012-10-23T01:48:08.340 回答