1

该函数uses将始终返回 True。如果我做对了,就只有12个字。

我如何使它uses返回False?

def count_vowels():
    fin = open ('words.txt')
    count = 0
    vowels = ['aeiou']
    for line in fin:
        word = line.strip()
        if uses(word, vowels):
            count = count + 1
            print "There are", count ," words with a vowel."

def uses(word, vowels):
    for vowels in word:
        if vowels in vowels:
            return True
    return False

def count_words():                                
    fin = open ('words.txt')
    vowels = ['aeiou']
    for line in fin:
            word = line.strip()
            if uses(word, vowels):
                print word

count_vowels()
count_words()

#This is what returns in the shell when I run my code:

>>>There are 1 words with a vowel.
There are 2 words with a vowel.
There are 3 words with a vowel.
There are 4 words with a vowel.
There are 5 words with a vowel.
....
#and so on...

# However, if I use the following code to replace the middle function in my original code:

def uses(word, vowels):
    found = ''
    for l in word:
        if l not in found:
            if l in vowels:
                found += l
        else:
            return False
    return found == vowels

# When I run the code the shell returns this:

>>>
>>>
4

5 回答 5

2

您可以简单地使用该all功能!让魔法起作用:

def uses(word, vowels):
    return all(letter in word for letter in vowels)

演示:

>>> s = 'aieou cdeaiou fgeaihou test doesnt work'
>>> for i in s.split():
    print (uses(i, 'aiueo'))


True
True
True
False
False
False

vowels有了这个,你作为一个字符串"aiueo"而不是传递['aiueo']

但是,如果您希望按顺序找到元音,请使用以下命令:

def uses(word, vowels):
    found = ''
    for l in word:
        if l not in found:
            if l in vowels:
                found += l
        else:
            return False
    return found == vowels

在这里,有点令人失望,但我只会给你完整的固定代码:

def count_vowels():               
    with open('words.txt') as fin:
        count = 0
        vowels = 'aeiou'
        for line in fin:
            words = line.split()
            for word in words:
                if uses(word, vowels):
                    count = count + 1
                    print "There are", count ," words with a vowel."

def uses(word, vowels):
    return all(letter in word for letter in vowels)

def count_words():                                
    with open('words.txt') as fin:
        vowels = 'aeiou'
        for line in fin:
                words = line.split()
                for word in words:
                    if uses(word, vowels):
                        print word

count_vowels()
count_words()
于 2013-11-09T17:36:38.030 回答
1

如果您只查找所有字母都aeiou按该顺序排列的单词(但可能中间有其他字母),您需要检查第一个元音直到找到它,然后检查第二个元音,然后是第三个,直到你找到它们(或到达单词的结尾)。这是一些执行此操作的代码:

def uses(word, vowels)
    vowel_index = 0
    for letter in word:
        if letter == vowels[vowel_index]:
            vowel_index += 1
            if vowel_index == len(vowels):
                return True
    return False

我没有删除找到的元音,而是简单地保留一个整数索引,告诉我目前正在测试哪个元音。

你会想用一个可下标的值来调用它vowels,要么是一个包含一个字符串作为其内容的列表,要么是一个包含所有元音的单个字符串。

于 2013-11-09T18:04:22.073 回答
0

如果我做对了,我会实现这样的uses-function:

def uses(word, vowels):
    # remove every letter, that's not a vowel, make the word lower case
    reduced_word = filter( lambda character: character in vowels, word.lower() )
    # check, whether the vowels are in order
    return vowels in reduced_word

vowels = "aeiou"

test_words = [ "atetitotu", "Atetitotu", "Haemodiutory", "FgeAihou" ]

for word in test_words: print word, uses(word, vowels)

输出:

atetitotu True
Atetitotu True
Haemodiutory False
FgeAihou False

但是如果你想找到一个文件的所有这些词,我会推荐re-module:

import re
file_content = open('file.txt').read()
vowels = "aeiou"
# the pattern starts with word boundary and there can be any kind of letters at the beginning
pattern = r"\b\w*"
# than between every vowel there can be any letter, except of a vowel
pattern += (r"[^%s]*?" % vowels ).join(vowels)
# and then, after the last vowel there can be any letter before the word ends
pattern += r"\w*\b"
# here the result: pattern = r"\b\w*a[^aeiou]*?e[^aeiou]*?i[^aeiou]*?o[^aeiou]*?u\w*\b"
# now we look for this pattern all over the file, ignoring case
re.findall(pattern, file_content, flags = re.I)

输出会是这样的

['atetitotu', 'Atetitotu']
于 2013-11-09T20:59:14.603 回答
0
def uses_only(wrd_trnsv, str_chars):
    for chr_trnsv in wrd_trnsv:
        if chr_trnsv not in str_chars:
            return False
    return True

wrd_list = open('C:\\Temp\\Complete_Words_list.txt')
def uses_all_txt(wrd_list, req_str_chrs):
    count = 0
    for each_wrd in wrd_list :
        if uses_only(req_str_chrs,each_wrd):
            count = count + 1
     return count
于 2016-03-28T06:58:17.770 回答
-1

就像@void 说的,使用re模块

import re
s = 'hello world'
words = re.findall(r'\w+', s)
print 'words'''
于 2013-11-09T17:35:14.933 回答