繁体   English   中英

如何使用两个 for 循环来检查列表中的行是否等于变量

[英]How do I use two for loops to check if line in list is equal to variable

对于个人项目,我需要使用两个 for 循环将文本文件中的数据提取到列表中。 之后,python 需要读取列表并检查变量lower_than_six ("password < 6")greater_than_ten ("password > 10")在代码中出现了多少次。 但由于某种原因,它没有这样做。 我写的代码:

def main():
    lower_than_six = "password < 6"
    greater_than_ten = "password > 10"
    input_file = "ITWorks_password_log.txt"
    f = open(input_file, "r")

    counter_pw_too_small = 0
    counter_pw_too_large = 0

    for line in f.readlines():
        attempts_list = line.strip()
        print(attempts_list)

    for line in attempts_list:
        if lower_than_six in line:
            counter_pw_too_small += 1
        elif greater_than_ten in line:
            counter_pw_too_large += 1

    print(f"\n Password < 6 occurred {counter_pw_too_small} times")
    print(f"\n Password > 10 occurred {counter_pw_too_large} times")

main()

您应该稍微修复一下代码。 像这样:

def main():
    lower_than_six = "password < 6"
    greater_than_ten = "password > 10"
    input_file = "ITWorks_password_log.txt"
    f = open(input_file, "r")

    counter_pw_too_small = 0
    counter_pw_too_large = 0

    attempts_list = []
    for line in f.readlines():
        attempt = line.strip()
        attempts_list.append(attempt)
        print(attempt)

    for line in attempts_list:
        if lower_than_six in line:
            counter_pw_too_small += 1
        elif greater_than_ten in line:
            counter_pw_too_large += 1

    print(f"\n Password < 6 occurred {counter_pw_too_small} times")
    print(f"\n Password > 10 occurred {counter_pw_too_large} times")

main()

您每次都将attempts_list定义为该行,因此最后它只是文件的最后一行。 相反,您需要启动它,然后是 append 行。 然后事情就会奏效。

顺便说一下,由于复制粘贴,我假设您的缩进就像这样,否则您需要修复缩进,以便 function 行位于 (main()) function 内。

我的测试文本文件:

blablalkdslkfvsdf
sdfglksdfglkpassword < 6dflgkld kfgdfg df
sdfgsd fgdfg sdfghpassword < 6dfghdgf 
sdfhgdfghsfdghfgb password < 6 ghs dgfh sdfghdfghdgfdsfgs
sdfg sdfg sdfg sdfghdfghdgfdsfgs
sdfg spassword > 10df 
sdfgsdghdfgh 

Output:

blablalkdslkfvsdf
sdfglksdfglkpassword < 6dflgkld kfgdfg df
sdfgsd fgdfg sdfghpassword < 6dfghdgf
sdfhgdfghsfdghfgb password < 6 ghs dgfh sdfghdfghdgfdsfgs
sdfg sdfg sdfg sdfghdfghdgfdsfgs
sdfg spassword > 10df
sdfgsdghdfgh

 Password < 6 occurred 3 times

 Password > 10 occurred 1 times

如果2 loops不是您需要完成的硬性要求,那么它很容易做到。 您可以只使用str.count() function 并打印出现的次数。

看一个例子:

lower_than_six = "password < 6"
greater_than_ten = "password > 10"
input_file = "ITWorks_password_log.txt"

file_content = open(input_file, "r").read()

print(f"\n Password < 6 occurred {file_content.count(lower_than_six)} times")
print(f"\n Password > 10 occurred {file_content.count(greater_than_ten)} times")

如果您需要使用循环,则 2 也不是必需的 - 您可以使用一个来完成。 喜欢,

def main():
    lower_than_six = "password < 6"
    greater_than_ten = "password > 10"
    input_file = "I1.txt"
    file_lines = open(input_file, "r").readlines()
    counter_pw_too_small = 0
    counter_pw_too_large = 0
    for line in file_lines:
        if lower_than_six in line:
            counter_pw_too_small += 1
        elif greater_than_ten in line:
            counter_pw_too_large += 1

    print(f"\n Password < 6 occurred {counter_pw_too_small} times")
    print(f"\n Password > 10 occurred {counter_pw_too_large} times")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM