为什么我找不到我正在查找的值

问题描述:

基本上,我想查找我在Python中读取的数据中有多少S和W。我是否正确地做,因为当我运行该程序时,它说有0 s和w的发现..这是不正确的。为什么我找不到我正在查找的值

这是我做了什么..

def input_function(): 
    my_file = open("TheData.txt", "r") 

    Data = [] 

    for each_line in my_file: 
     Data.append(each_line.split()) 
     print (Data) 
    return Data 


def Number_Of_S(Data): 
    CountS= 0 
    for x in range(len(Data)): 
     if Data[x] == ("S"): 
      CountS = CountS + 1 
    print ("There are " + str(CountS) + " S's") 
    return CountS 


def Number_Of_W(Data): 
    CountW= 0 
    for x in range(len(Data)): 
     if Data[x] == ("W"): 
      CountW = CountW + 1 
    print ("There are " + str(CountW) + " W's") 

    return CountW 

#Main program 
Data = input_function() 
Number_Of_W = Number_Of_W(Data) 
Number_Of_S = Number_Of_S(Data) 
+0

为什么不你使用内建的['list.count'](https://docs.python.org/3/tutorial/datastructures.html)方法? – soon

+0

你的方法适合我。你的''TheData.txt''文件是什么样的? –

什么读取整个文件转换成字符串,然后使用内置函数计算字符字符串中?

with open('test.txt', 'r') as content_file: 
    content = content_file.read() 

sCount = content.count('S') 
wCount = content.count('W') 

print ("There are " + str(sCount) + " S's") 
print ("There are " + str(wCount) + " S's") 

您只是创建了错误的数组。您的清单是例如

['Wossd', 'sds', 'dsd', 's', 'd'] 
['Ssd', 'sds', 'dsd', 's', 'd'] 
['sdsdsd'] 

但对于你的计数功能应该是

['W', 'o', 's', 's', 'd', ' ', 's', 'd', 's', ' ', 'd', 's', 'd', ' ', 's', ' ', 'd'] 
['S', 's', 'd', ' ', 's', 'd', 's', ' ', 'd', 's', 'd', ' ', 's', ' ', 'd'] 
['s', 'd', 's', 'd', 's', 'd'] 

修改您的输入功能

def input_function(): 
    my_file = open("TheData.txt", "r") 

    Data = [] 

    for each_line in my_file: 
     Data = Data + list(each_line.strip()) 
    return Data 

所以输出

There are 1 W's 
There are 1 S's 

那么代码的其余部分作品,但有一个很多简单的解决方案

你的计数功能工作得很好:

>>> def Number_Of_S(Data): 
    CountS= 0 
    for x in range(len(Data)): 
     if Data[x] == ("S"): 
      CountS = CountS + 1 
    print ("There are " + str(CountS) + " S's") 
    return CountS 

>>> 
>>> Number_Of_S('HASSAH') 
There are 2 S's 
2 

问题是与你的input_function(),它创建一个列表的列表(第一每条线分割成一个列表,然后将这些名单附加到Data列表)。它应该仅仅是行的列表,或可选择您应该通过的话在你Number_Of_S使用三个嵌套循环for循环:

for line in Data: # Line is a list of words 
    for word in line: # Word is a string 
     for character in word: 
      if character == 'S': 
       CountS += 1 # Increment by one 

一些其他方面的改进,完全无关,您的问题。你的编码风格相当宽松。我不知道你从哪里学到了它,但我建议你尝试根据“一般”建议进行调整,例如snake_case_variable_and_function_names而不是Capitalized_Names

此外,除非没有其他方法,否则请勿使用range()for循环。在Python中,您可以使用for element in my_list:直接循环列表。随着这些变化在心中,这就是我想你的职责应该是这样的:

def number_of_s(data): 
    count_s = 0 
    for char in data: 
     if char == 'S': 
      count_s += 1 # Short for count_s = count_s + 1 
    return count_s 

它强烈建议做印刷的功能之外:和

>>> my_data = 'HASSAH' 
>>> count_s = number_of_s(my_data) 
>>> print("There are " + str(count_s) + " S's") 

,而不是具有多种功能每个字母,为什么不使用一个带有两个参数:

def number_of_characters(string, character): 
    count = 0 
    for char in string: 
     if char == character: 
      count += 1 
    return count 

最后,我相信这只是一个实践项目,因此,这是好的,但如果你将永远使用这个我在它

>>> my_data = 'HASSAH' 
>>> count_s = my_data.count('S') 
>>> print("There are " + str(count_s) + " S's") 

而更加最后,你应该阅读整个文件一次,并且数字符:NA真正的程序,你应该使用list.count()str.count()代替

with open('TheData.txt') as infile: 
    text = infile.read() 
print("There are " + str(text.count('S')) + " S's")