为什么我找不到我正在寻找的价值

时间:2016-03-02 09:12:02

标签: python

基本上,我想知道有多少S和W在我用Python阅读的数据中。我是否正确地做了,因为当我运行程序时,它说有0秒,并且找到了......这不是真的。

这就是我所做的......

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)

3 个答案:

答案 0 :(得分:0)

如何将整个文件读入字符串,然后使用内置函数计算字符串中的字符?

J:\SymetricDS\symmetric-server-3.7.28\engines

答案 1 :(得分:0)

您的计数功能正常工作:

>>> 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列表中)。它应该只是一个行列表,或者您可以选择在for中使用三个嵌套的Number_Of_S循环来循环显示这些词:

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

最后,我相信这只是一个练习程序,所以这很好,但是如果你在真正的程序中使用它,你应该使用list.count()str.count()代替:

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

最后,您应该立即阅读整个文件并计算其中的字符:

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

答案 2 :(得分:-1)

您只是创建了错误的数组。你的清单就是这样。

['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

然后其余的代码工作,但有很多更简单的解决方案