![](/img/trans.png)
[英]Pyspark how to count the number of occurences of a string in each group and print multiple selected columns?
[英]Python count occurences of a string and print the lines that contain them, as well as print number of occurences of string, with multiple clauses
我一直在尝试创建一个 python 脚本,它接受两个输入,一个文件名和一个字符串。 一旦将这些输入,它应该打印出输入字符串的出现次数,以及包含输入字符串的每一行。
我还被要求不使用列表、split 方法、python 字符串方法、关键字“in”,并且我可能只使用索引来访问字符串的第一个字符,并使用切片来获取字符串的尾部。
到目前为止我所做的:
def main():
search_for = raw_input("What term would you like to search for?")
text_file_name = raw_input("Which file would you like to search in?")
count_text_file(search_for, text_file_name)
def count_text_file(search_for, text_file_name):
usersFile = open(text_file_name, 'r')
usersTermLength = len(search_for)
usersFileLength = len(text_file_name)
occurenceOfString = 0
while i<usersFileLength:
firstChar = usersFile[i]
if firstChar==searchFor[0]:
indexUsersTermLength = usersTermLength + i #end slice
possibleMatch = usersFile[i:indexUsersTermLength]
if possibleMatch == searchFor:
print #the entire line
occurenceOfString+=1
i+=1
else:
i+=1
else:
i+=1
您的代码中的一些问题。
usersFileLength = len(text_file_name)
这只是文件名的长度。 不是文件内容的大小。
firstChar = usersFile[i]
这不是您从文件中读取的方式。 您需要使用read()
类的函数。
此外,您打破了一些(愚蠢的)约束。 这是我的解决方案。 它读取整个文件,然后逐个字符地遍历它。 它构建当前单词,当它到达一个非字母时进行比较。
def count_text_file(search_for, text_file_name):
with open(text_file_name, 'r') as users_file:
# Read entire file
content = users_file.read()
line_number = 1
# Build the words of the file char-by-char
current_word = ""
while len(content) > 0:
# "only use indexing to access the first character of a string"
c = content[0]
# If it's a letter add to string
# Can't use c.isalpha() as it is a "python string method"
if (c >= 'A' and c <= 'Z') or (c >= 'a' and c <= 'z'):
current_word += c
# Else (not a letter), check the word
else:
if current_word == search_for:
print(f"found at line {line_number}")
if c == '\n':
line_number += 1
# Reset for next word
current_word = ""
# "only use ... slicing to get the tail of the string"
content = content[1:]
您可以进行一些改进。 例如,找不到标点符号的单词(例如:“不能”或“不存在”)。 此外,它只将“字母”视为“[A-Za-z]”。 Unicode 字符将无法识别。 并且区分大小写。 但既然这是一项作业,谁知道你的老师是否关心这些。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.