Skip to content
Advertisement

Is there a way to make this “for loop” cleaner in Python?

I created a simple script that goes to a notepad file, reads it line by line and if it finds certain words it adds to a counter. Then at the end it spits out the final count for each word.

The current implementation works, I just know it’s not the most efficient way of doing things. I would love for someone with more experience to see if there is a simple FOR loop that can reduce the need of having 60 rows of the same thing. I am currently very inexperienced in Python and I’d love to find more efficient ways to do the same thing, this would help me learn new things

The code is here (it’s long but just because I’m writing everything out manually, my goal is to accomplish the same thing with just a few lines using for loops)

location = "X:SalesShortcutsShiftReportsIL.txt"
rep1 = "ttsachev"
rep2 = "vpopov"
rep3 = "alupashko"
rep4 = "ekarachorova"
rep5 = "glipchev"
rep6 = "ggeorgiev"
rep7 = "syovcheva"
rep8 = "vpanchev"
rep9 = "vbimbalova"
rep10 = "hmarinov"
rep11 = "fr-egonzalez"
rep12 = "dvaldenegro"
rep13 = "ndinev"
rep14 = "apiera"
rep15 = "csehunoe"
rep16 = "dbolingo"
rep17 = "mmamatela"
rep18 = "enter new rep here"
rep19 = "enter new rep here"


count = "count"

def Count():

        # setting the count of each "rep" to 0

        rep1count = 0
        rep2count = 0
        rep3count = 0
        rep4count = 0
        rep5count = 0
        rep6count = 0
        rep7count = 0
        rep8count = 0
        rep9count = 0
        rep10count = 0
        rep11count = 0
        rep12count = 0
        rep13count = 0
        rep14count = 0
        rep15count = 0
        rep16count = 0
        rep17count = 0
        rep18count = 0
        rep19count = 0


        global locationIL
        global locationAU
        global locationES

        # opening the first txt file
        
        with open(locationIL, 'r', encoding="utf8",  errors='ignore') as f:
                for line in f.readlines():
                        words = line.lower().split() 

                       # main for loop, going over each line and checking if the 
                       # username of each employee is present. If it is, it adds 
                       # 1 to that person's counter.

                        for word in words: 
                                if word == rep1:
                                        rep1count += 1
                                elif word == rep2:
                                        rep2count += 1
                                elif word == rep3:
                                        rep3count += 1
                                elif word == rep4:
                                        rep4count += 1
                                elif word == rep5:
                                        rep5count += 1
                                elif word == rep6:
                                        rep6count += 1
                                elif word == rep7:
                                        rep7count += 1
                                elif word == rep8:
                                        rep8count += 1
                                elif word == rep9:
                                        rep9count += 1
                                if word == rep10:
                                        rep10count += 1
                                elif word == rep11:
                                        rep11count += 1
                                elif word == rep12:
                                        rep12count += 1
                                elif word == rep13:
                                        rep13count += 1
                                elif word == rep14:
                                        rep14count += 1
                                elif word == rep15:
                                        rep15count += 1
                                elif word == rep16:
                                        rep16count += 1
                                elif word == rep17:
                                        rep17count += 1
                                elif word == rep18:
                                        rep18count += 1
                                elif word == rep19:
                                        rep19count += 1

        f.close

Thank you!

Advertisement

Answer

you could use a list for your items then a dict for the counts. for each rep in the counts dict check if it exists in the line.

location = "myfile.txt"
reps = ["ttsachev", "vpopov", "alupashko", "ekarachorova", "glipchev",
        "ggeorgiev", "syovcheva", "vpanchev", "vbimbalova",
        "hmarinov", "fr-egonzalez", "dvaldenegro", "ndinev", "apiera", "csehunoe",
        "dbolingo", "mmamatela", "enter new rep here"]


def count():
    # setting the count of each "rep" to 0
    rep_counts = {rep: 0 for rep in reps}

    # opening the first txt file

    with open(location, 'r', encoding="utf8", errors='ignore') as f:
        for line in f.readlines():
            words = line.lower().split()
            for rep in rep_counts:
                if rep in line.lower():
                    rep_counts[rep] += 1
    return rep_counts


counts = count()
print(counts)

SAMPLE DATA

this is a line hmarinov wth data
this is another mmamatela

OUTPUT

{'ttsachev': 0, 'vpopov': 0, 'alupashko': 0, 'ekarachorova': 0, 'glipchev': 0, 'ggeorgiev': 0, 'syovcheva': 0, 'vpanchev': 0, 'vbimbalova': 0, 'hmarinov': 1, 'fr-egonzalez': 0, 'dvaldenegro': 0, 'ndinev': 0, 'apiera': 0, 'csehunoe': 0, 'dbolingo': 0, 'mmamatela': 1, 'enter new rep here': 0}
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement