簡體   English   中英

有沒有辦法讓 Python 中的這個“for循環”更干凈?

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

我創建了一個簡單的腳本,它轉到記事本文件,逐行讀取,如果找到某些單詞,它會添加到計數器中。 然后在最后它吐出每個單詞的最終計數。

當前的實現有效,我只知道這不是最有效的做事方式。 我希望有更多經驗的人看看是否有一個簡單的 FOR 循環可以減少對 60 行相同事物的需求。 我目前在 Python 方面非常缺乏經驗,我很想找到更有效的方法來做同樣的事情,這將有助於我學習新事物

代碼在這里(它很長,但只是因為我手動編寫所有內容,我的目標是使用 for 循環只用幾行代碼來完成同樣的事情)

location = "X:\Sales\Shortcuts\ShiftReportsIL.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

謝謝!

您可以為您的項目使用一個列表,然后為計數使用一個字典。 對於counts dict中的每個rep,檢查它是否存在於行中。

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)

樣本數據

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}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM