如何检测'\n';是否存在于python列表中?

如何检测'\n';是否存在于python列表中?,python,Python,我有一份由一些项目组成的清单。其中一项是\n。我想使用if条件检查列表末尾是否包含“\n.”,但它不起作用 content=['abc','def','ghi','jkl','\n.] 如果内容[-1]==(“\n.”或“\\n.”) 打印('它有效') 其他: 打印('它不') 使用以下方法: content=['abc','def','ghi','jkl','\n.] 如果内容中有“\n.”或内容中有“\\n.”:打印('It works') else:print('它不') 您可以使用以下

我有一份由一些项目组成的清单。其中一项是\n。我想使用if条件检查列表末尾是否包含“\n.”,但它不起作用

content=['abc','def','ghi','jkl','\n.]
如果内容[-1]==(“\n.”或“\\n.”)
打印('它有效')
其他:
打印('它不')
使用以下方法:

content=['abc','def','ghi','jkl','\n.]
如果内容中有“\n.”或内容中有“\\n.”:打印('It works')
else:print('它不')

您可以使用以下代码。此代码可以识别列表中的
\n.
\\n.
,并且可以检查它们是否是列表中的最后一个元素

代码:

content = ["abc", "def", "ghi", "jkl", "\n."]
if "\n." in content:
    print(r"\n in list")
    if "\n." == content[-1]:
        print(r"\n is the last element in list")
    else:
        print(r"\n is not the last element in list")
elif "\\n." in content:
    print(r"\\n in list")
    if "\\n." == content[-1]:
        print(r"\\n is the last element in list")
    else:
        print(r"\\n is not the last element in list")
else:
    print(r"\n or \\n are not in list")
>>> python3 test.py
\n in list
\n is the last element in list
>>> python3 test.py
\n in list
\n is not the last element in list
>>> python3 test.py
\\n in list
\\n is the last element in list
>>> python3 test.py
\\n in list
\\n is not the last element in list
>>> python3 test.py
\n or \\n are not in list
输出:

content = ["abc", "def", "ghi", "jkl", "\n."]
if "\n." in content:
    print(r"\n in list")
    if "\n." == content[-1]:
        print(r"\n is the last element in list")
    else:
        print(r"\n is not the last element in list")
elif "\\n." in content:
    print(r"\\n in list")
    if "\\n." == content[-1]:
        print(r"\\n is the last element in list")
    else:
        print(r"\\n is not the last element in list")
else:
    print(r"\n or \\n are not in list")
>>> python3 test.py
\n in list
\n is the last element in list
>>> python3 test.py
\n in list
\n is not the last element in list
>>> python3 test.py
\\n in list
\\n is the last element in list
>>> python3 test.py
\\n in list
\\n is not the last element in list
>>> python3 test.py
\n or \\n are not in list

如果您的
内容
列表为:

content = ["abc", "def", "ghi", "jkl", "\n.", "asdf"]
content = ["abc", "def", "ghi", "jkl", "\\n."]
content = ["abc", "def", "ghi", "jkl", "\\n.", "asdf"]
content = ["abc", "def", "ghi", "jkl", "asdf"]
输出:

content = ["abc", "def", "ghi", "jkl", "\n."]
if "\n." in content:
    print(r"\n in list")
    if "\n." == content[-1]:
        print(r"\n is the last element in list")
    else:
        print(r"\n is not the last element in list")
elif "\\n." in content:
    print(r"\\n in list")
    if "\\n." == content[-1]:
        print(r"\\n is the last element in list")
    else:
        print(r"\\n is not the last element in list")
else:
    print(r"\n or \\n are not in list")
>>> python3 test.py
\n in list
\n is the last element in list
>>> python3 test.py
\n in list
\n is not the last element in list
>>> python3 test.py
\\n in list
\\n is the last element in list
>>> python3 test.py
\\n in list
\\n is not the last element in list
>>> python3 test.py
\n or \\n are not in list

如果您的
内容
列表为:

content = ["abc", "def", "ghi", "jkl", "\n.", "asdf"]
content = ["abc", "def", "ghi", "jkl", "\\n."]
content = ["abc", "def", "ghi", "jkl", "\\n.", "asdf"]
content = ["abc", "def", "ghi", "jkl", "asdf"]
Otuput:

content = ["abc", "def", "ghi", "jkl", "\n."]
if "\n." in content:
    print(r"\n in list")
    if "\n." == content[-1]:
        print(r"\n is the last element in list")
    else:
        print(r"\n is not the last element in list")
elif "\\n." in content:
    print(r"\\n in list")
    if "\\n." == content[-1]:
        print(r"\\n is the last element in list")
    else:
        print(r"\\n is not the last element in list")
else:
    print(r"\n or \\n are not in list")
>>> python3 test.py
\n in list
\n is the last element in list
>>> python3 test.py
\n in list
\n is not the last element in list
>>> python3 test.py
\\n in list
\\n is the last element in list
>>> python3 test.py
\\n in list
\\n is not the last element in list
>>> python3 test.py
\n or \\n are not in list

如果您的
内容
列表为:

content = ["abc", "def", "ghi", "jkl", "\n.", "asdf"]
content = ["abc", "def", "ghi", "jkl", "\\n."]
content = ["abc", "def", "ghi", "jkl", "\\n.", "asdf"]
content = ["abc", "def", "ghi", "jkl", "asdf"]
Otuput:

content = ["abc", "def", "ghi", "jkl", "\n."]
if "\n." in content:
    print(r"\n in list")
    if "\n." == content[-1]:
        print(r"\n is the last element in list")
    else:
        print(r"\n is not the last element in list")
elif "\\n." in content:
    print(r"\\n in list")
    if "\\n." == content[-1]:
        print(r"\\n is the last element in list")
    else:
        print(r"\\n is not the last element in list")
else:
    print(r"\n or \\n are not in list")
>>> python3 test.py
\n in list
\n is the last element in list
>>> python3 test.py
\n in list
\n is not the last element in list
>>> python3 test.py
\\n in list
\\n is the last element in list
>>> python3 test.py
\\n in list
\\n is not the last element in list
>>> python3 test.py
\n or \\n are not in list

如果您的
内容
列表为:

content = ["abc", "def", "ghi", "jkl", "\n.", "asdf"]
content = ["abc", "def", "ghi", "jkl", "\\n."]
content = ["abc", "def", "ghi", "jkl", "\\n.", "asdf"]
content = ["abc", "def", "ghi", "jkl", "asdf"]
Otuput:

content = ["abc", "def", "ghi", "jkl", "\n."]
if "\n." in content:
    print(r"\n in list")
    if "\n." == content[-1]:
        print(r"\n is the last element in list")
    else:
        print(r"\n is not the last element in list")
elif "\\n." in content:
    print(r"\\n in list")
    if "\\n." == content[-1]:
        print(r"\\n is the last element in list")
    else:
        print(r"\\n is not the last element in list")
else:
    print(r"\n or \\n are not in list")
>>> python3 test.py
\n in list
\n is the last element in list
>>> python3 test.py
\n in list
\n is not the last element in list
>>> python3 test.py
\\n in list
\\n is the last element in list
>>> python3 test.py
\\n in list
\\n is not the last element in list
>>> python3 test.py
\n or \\n are not in list

如果内容[-1]='\n.'或内容[-1]='\\n'