在python中可以写下单条if条件吗?

Gal*_*hok 0 python python-3.x python-requests

如果使用Python 3.7中的条件,如何仅使用单个if条件而不是两个来重写下面的代码?

with open('demo.csv', 'r') as f, open("Result_csv.csv", 'w+') as out:
    for line in f:
        if '/tcp' in line:
            print(line)
            out.write(line)
        if '/udp' in line:
            print(line)
            out.write(line)
Run Code Online (Sandbox Code Playgroud)

Nem*_*Est 5

with open('demo.csv', 'r') as f, open("Result_csv.csv", 'w+') as out:
    for line in f:
        if '/tcp' in line or '/udp' in line:
            print(line)
            out.write(line)
Run Code Online (Sandbox Code Playgroud)

  • 括号可以说它不那么清晰,也违反了风格指南,但它仍然可以正常工作. (2认同)