繁体   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