My goal is to count occurrences of a substring in a list of strings
passwordlog.txt:
2022-09-12 03:22:18.170604, password < 6
2022-09-12 03:22:33.878446, password > 10
2022-09-12 03:22:40.686602, password > 10
My program:
# Create list from contents of log file
log_list = open("passwordlog.txt").read().splitlines()
# Print lines of list until end of list
for i in range(0, len(log_list)):
print(log_list[i])
# Output count of passwords below minimum and above maximum length
print("Amount of invalid passwords below minimum length:", sum("< 6" in i for i in log_list))
print("Amount of invalid passwords above maximum length:", sum("> 10" in i for i in log_list))
I understand the majority of this code, but the first and final lines are responsible for immense confusion in me. They produce the intended results, but I don't understand how they work, particularly the inclusion of ".read().splitlines()" and "in i for i in".
To be very clear I'm quite a beginner so please explain these parts of the code as explicitly as you can. Maybe there's also a more beginner-friendly method that could replace these first and last two lines that you could also please explain as explicitly as possible