如何在python中从文件中搜索字符 实际上,我是来自C++的,我也是新来的,我有迭代问题。我使用Python 2.7.8,无法解决我想要的。我有一个名为“foo.txt”的文件名。通过代码,我试图通过使用文件中有多少个“EAIOU”来查找。我已经创建了数组:元音[]={'a','e','I','o',u},我的代码应该shd给我所有元音的组合计数。但我面对的是

如何在python中从文件中搜索字符 实际上,我是来自C++的,我也是新来的,我有迭代问题。我使用Python 2.7.8,无法解决我想要的。我有一个名为“foo.txt”的文件名。通过代码,我试图通过使用文件中有多少个“EAIOU”来查找。我已经创建了数组:元音[]={'a','e','I','o',u},我的代码应该shd给我所有元音的组合计数。但我面对的是,python,Python,错误: TypeError: list indices must be integers, not str 文件foo.txt Chronobiology might sound a little futuristic – like something from a science fiction novel, perhaps – but it’s actually a field of study that concerns one of the oldest processes life

错误:

TypeError: list indices must be integers, not str
文件foo.txt

Chronobiology might sound a little futuristic – like something from a science fiction novel, perhaps – but it’s actually a field of study that concerns one of the oldest processes life on this planet has ever known: short-term rhythms of time and their effect on flora and fauna.

This can take many forms. Marine life, for example, is influenced by tidal patterns. Animals tend to be active or inactive depending on the position of the sun or moon. Numerous creatures, humans included, are largely diurnal – that is, they like to come out during the hours of sunlight. Nocturnal animals, such as bats and possums, prefer to forage by night. A third group are known as crepuscular: they thrive in the low-light of dawn and dusk and remain inactive at other hours.

When it comes to humans, chronobiologists are interested in what is known as the circadian rhythm. This is the complete cycle our bodies are naturally geared to undergo within the passage of a twenty-four hour day. Aside from sleeping at night and waking during the day, each cycle involves many other factors such as changes in blood pressure and body temperature. Not everyone has an identical circadian rhythm. ‘Night people’, for example, often describe how they find it very hard to operate during the morning, but become alert and focused by evening. This is a benign variation within circadian rhythms known as a chronotype.
我的代码:

fo = open("foo.txt", "r")
count = 0
for i in fo:
    word = i
    vowels = ['a','e','i','o','u','y']
    word = word.lower().strip(".:;?!")
#print word 
for j in word: # wanting that loop shd iterate till the end of file
    for k in vowels: # wanting to index string array until **vowels.length()**
        if (vowels[k] == word[j]):
            count +=1


#print word[0]    
print count
改为使用范围(len()),因为如果在元音中使用
表示k,k将是“a”然后是“b”然后是“c”。。。然而,通过索引获取对象的语法是元音[index_number],而不是元音[content]。因此,您必须迭代数组的长度,使用元音[0]获得“a”,然后使用元音[1]”获得“b”,等等

fo = open("foo.txt", "r")
count = 0
for i in fo:
    word = i
    vowels = ['a','e','i','o','u','y']
    word = word.lower().strip(".:;?!")
#print word 

    for j in range(len(word)): # wanting that loop shd iterate till the end of file
        if (word[j] in vowels):
                count +=1


#print word[0]    
print count
改为使用范围(len()),因为如果在元音中使用
表示k,k将是“a”然后是“b”然后是“c”。。。然而,通过索引获取对象的语法是元音[index_number],而不是元音[content]。因此,您必须迭代数组的长度,使用元音[0]获得“a”,然后使用元音[1]”获得“b”,等等

fo = open("foo.txt", "r")
count = 0
for i in fo:
    word = i
    vowels = ['a','e','i','o','u','y']
    word = word.lower().strip(".:;?!")
#print word 

    for j in range(len(word)): # wanting that loop shd iterate till the end of file
        if (word[j] in vowels):
                count +=1


#print word[0]    
print count

Python以其抽象和标准库数据结构而自豪。签出
集合。计数器
。它接受一个iterable并返回一个value->frequency的dict

with open('foo.txt') as f:
    string = f.read()

counter = collections.Counter(string)  # a string is an iterable of characters
vowel_counts = {vowel: counter[vowel] for vowel in "aeiou"} 

Python以其抽象和标准库数据结构而自豪。签出
集合。计数器
。它接受一个iterable并返回一个value->frequency的dict

with open('foo.txt') as f:
    string = f.read()

counter = collections.Counter(string)  # a string is an iterable of characters
vowel_counts = {vowel: counter[vowel] for vowel in "aeiou"} 

Python有一个很棒的模块,名为
collections
,带有一个函数。您可以这样使用它:

import collections
with open('foo.txt') as f:
    letters = collections.Counter(f.read())
vowels = ['a','e','i','o','u','y']
## you just want the sum
print(sum(letters[vowel] for vowel in vowels))
您也可以在不使用集合的情况下执行此操作。计数器()

请注意,集合
{}
查找的时间复杂度为
O(1)
,而列表
[]
查找的时间复杂度为
O(n)

我用模块测试了这两种方法,正如预期的那样,第一种方法使用的是
collections.Counter()
稍快一些:

0.13573385099880397
0.16710168996360153

Python有一个很棒的模块,名为
collections
,带有一个函数。您可以这样使用它:

import collections
with open('foo.txt') as f:
    letters = collections.Counter(f.read())
vowels = ['a','e','i','o','u','y']
## you just want the sum
print(sum(letters[vowel] for vowel in vowels))
您也可以在不使用集合的情况下执行此操作。计数器()

请注意,集合
{}
查找的时间复杂度为
O(1)
,而列表
[]
查找的时间复杂度为
O(n)

我用模块测试了这两种方法,正如预期的那样,第一种方法使用的是
collections.Counter()
稍快一些:

0.13573385099880397
0.16710168996360153

在Python中,几乎没有理由索引到for循环中的字符串中——您可以直接对它们进行迭代。例如:
用于word中的字符:
。这里的
word
是一行,而不是文件
foo.txt
中的一个单词。在Python中,几乎没有理由在for循环中索引到字符串中——您可以直接迭代它们。例如:
用于word中的字符:
。此处
word
是文件
foo.txt
中的一行而不是一个单词。谢谢,但我得到的计数较少,为206,实际计数超过400。知道吗?识别正确吗?我为您更正了代码以检查是否正确。这里的
word
是一行,而不是文件
foo.txt
中的一个单词。谢谢,但我得到的计数较少,为206,实际计数超过400。知道吗?识别正确吗?我为您更正了代码以检查它是否正确。这里的
word
是一行,而不是文件
foo.txt
中的一个单词。感谢它这么做,但我也尝试查找辅音,通过跟踪您,它给了我错误类型error:unhable type:'list'导入集合,open('foo.txt')为f:lets=collections.Counter(f.read())元音=['a'、'e'、'i'、'o'、'u']辅音=['b'、'c'、'd'、'f'、'g'、'h'、'j'、'k'、'l'、'm'、'n'、'p'、'q'、'r'、's'、't'、'v'、'w'、'x'、'y'、'z']#您只需要打印和(和(和[字母[元音中的元音字母]))(和[和])打印辅音字母[和](和]打印辅音字母[和](和]打印辅音字母[和](和]需要打印辅音字母[和]打印(sum(字母[v]表示辅音中的v))