Home > other >  Confusion over code counting occurrences of a substring in a list of strings
Confusion over code counting occurrences of a substring in a list of strings

Time:09-13

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

CodePudding user response:

Well there is many ways to change this code and make it more beginner friendly.

You can split the first line in multiple, easier, lines.

smallerSix = 0
greaterTen = 0

with open("passwordlog.txt") as log_file:
    lines = log_file.read()      
    lines = lines.splitlines()
    for item in lines:
        if "< 6" in item:
            smallerSix  = 1
        if "> 10" in item:
            greaterTen  = 1

print("Amount of invalid passwords below minimum length:", smallerSix)
print("Amount of invalid passwords above maximum length:", greaterTen)

with open(".txt") as x opens a file and creates a reference x. With that reference you can edit the file content or read the file content out.

lines = log_file.read() reads out the content of the log_file and copyies it's values into lines

lines = lines.splitlines() splits the String lines at every row which creates a list. Each "place" of the list contains a line (String)

Now you have a list that has every line of the log_file. Next step is to search each line for a "< 6" and a "> 10". We can do that by going through each element of the array and check the string for this specific string. To do that we can loop through each element of the list: for item in lines. Now we check each item for the specific string "< 6" and "> 10". If one of them is in item, then the variable that contributes to it is being increased by 1.

At the end you just print out the variable values.

  • Related