[英]Count occurrences from a list Python
I have to write a program that reads from a file, appends the data to a list, reads the list and outputs the number of times the following errors appear "password < 6" and "password > 10"我必须编写一个程序,从文件中读取数据,将数据附加到列表中,读取列表并输出以下错误出现“密码 < 6”和“密码 > 10”的次数
I am having trouble working out the code to get the program to read the strings for those specific words and output how many times they appear.我无法编写代码以使程序读取这些特定单词的字符串以及 output 它们出现的次数。
def main():
PASSWORD_LOG_FILE = "ITWorks_password_log.txt"
list_data = []
input_file = open(PASSWORD_LOG_FILE, "r")
for line in input_file:
list_data.append(line)
input_file.close()
for error in list_data:
print(error, end="")
for error in range(0, len(list_data)):
count_pw_too_small = list_data.count("password < 6")
count_pw_too_large = list_data.count("password > 10")
print("\nThe number of passwords that were under the minimum length: ", count_pw_too_small)
print("The number of passwords that were over the maximum length: ", count_pw_too_large)
main()
And below is the data from the list its supposed to read:以下是它应该读取的列表中的数据:
2019-07-22 16:24:42.843103, password < 6 2019-07-22 16:24:42.843103, 密码 < 6
After speaking with my lecturer we have come to following solution, which doesn't use the count method but still worked perfectly.在与我的讲师交谈后,我们得出了以下解决方案,该解决方案不使用计数方法,但仍然可以完美运行。
def main():
PASSWORD_LOG_FILE = "ITWorks_password_log.txt"
list_data = []
input_file = open(PASSWORD_LOG_FILE, "r")
for line in input_file:
list_data.append(str(line))
input_file.close()
count_pw_too_small = 0
count_pw_too_large = 0
for error in list_data:
print(error, end="")
if "< 6" in error:
count_pw_too_small += 1
elif "> 10" in error:
count_pw_too_large += 1
print("\nThe number of passwords that were under the minimum length: ", count_pw_too_small)
print("The number of passwords that were over the maximum length: ", count_pw_too_large)
main()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.