首页 > 解决方案 > 使用 not in 运算符打破 python 循环

问题描述

此循环未按预期工作。它打印所有 4 个元素(预计 3 个)

guj = ["बळींचाी", "आठ़वले", "मोठय़ा", "धाांची"]

for y in guj:
    for i in y:
        x = i.encode("raw_unicode_escape")
        if x[:5][-3:] not in [b"090", b"091", b"092", b"093", b"094"]:
            break
    print(y)

不应打印第三个单词,因为它包含一个无效的 095 系列字符。

# "य़".encode("raw_unicode_escape") # b'\\u095f'

输出.txt

बळींचाी
आठ़वले
मोठय़ा
धाांची

预期的.txt

बळींचाी
आठ़वले
धाांची

标签: pythonloopsbreak

解决方案


推荐阅读