【问题标题】:Is there a way to make this "for loop" cleaner in Python?有没有办法在 Python 中使这个“for循环”更干净?
【发布时间】:2021-03-30 13:21:51
【问题描述】:

我创建了一个简单的脚本,它转到记事本文件,逐行读取,如果找到某些单词,它就会添加到计数器中。然后在最后吐出每个单词的最终计数。

当前的实现有效,我只知道这不是最有效的做事方式。我希望有更多经验的人看看是否有一个简单的 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

谢谢!

【问题讨论】:

  • 那么您是否考虑过使用list 来包含您的数据?
  • 不,我没有。我考虑过使用字典,但它的第一个实现并不太奏效,所以我手动编写所有内容
  • 对于初学者,请参阅“in”关键字stackoverflow.com/questions/3437059/…。然后你可以得到一个你正在寻找的字符串列表,然后让你的 for 循环遍历那个列表。
  • 所以我用谷歌搜索它,它看起来像一本字典。那么是将员工的姓名放在一个列表中,然后使用 for 循环遍历列表中的每个项目,而不是每次手动添加它们?这在实践中如何运作?
  • @ScottHoward 我将尝试使用列表、字典和“in”重新创建脚本,看看效果如何!

标签: python for-loop counter


【解决方案1】:

您可以为您的项目使用一个列表,然后为计数使用一个字典。对于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

输出

{'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}

【讨论】:

  • 这就是我想要的!简单的 for 循环 + 字典,让你用 4 行代码而不是 60 行代码来做某事
猜你喜欢
  • 2022-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多