繁体   English   中英

使用 endswith() 检查一个列表中的字符串是否以另一个列表中的字符串结尾?

[英]Use endswith() to check if a string in one list ends with a string from another list?

我需要检查一个列表中的项目是否列在另一个列表中的字符串末尾,最好以某种方式使用endswith()但我不确定如何仅使用这种方法来解决它,而不是让它取决于长度其他列表等。

对于单个列表,这非常有效:

mylist = ["item 1 a", "item 2 a", "item 3 b"]

for item in mylist:
    if item.endswith("a"):
        print(f"{item} ends with 'a'")
    else:
        print(f"{item} ends with 'b'")

# Output: 
# item 1 a ends with 'a'
# item 2 a ends with 'a'
# item 3 b ends with 'b'

但是假设我有 2 个列表,我不确定如何检查一个列表中的列表项是否以另一个列表中的选项之一结尾。 由于endswith()只返回 True 或 False,我完全意识到下面的代码是错误的,但它更多地是展示我希望它如何工作的一种方式:

mylist = ["item 1", "item 2", "item 3"]
newlist = ["added item 1", "added item 3"]

for item in mylist:
    if item.endswith(item) in newlist:
# Wont work because the return is a boolean
# value, just an example of what I'm looking for'
        print(f"newlist has {item} added")
    else:
        print(f"newlist still waiting for {item}")

我知道还有其他方法可以获得相同的结果,但是有没有什么方法可以做到这一点而不需要使用正则表达式或另一个列表来存储不同的字符串并将其基于endswith()

您的示例代码有点令人困惑,但是如果您想检查mylist中的哪些项目是newlist中任何字符串的后缀,则以下内容应该可以工作:

mylist = ["item 1", "item 2", "item 3"]
newlist = ["added item 1", "added item 3"]

for item in mylist:
    found = False
    
    for newitem in newlist:
       if newitem.endswith(item):
           found = True
           break

    if found:
       print(f"newlist has {item} added")
    else:
       print(f"newlist still waiting for {item}")

这段代码的输出如下,我猜这就是你想要的:

newlist has item 1 added
newlist still waiting for item 2
newlist has item 3 added

如果我理解正确,这应该是解决方案

mylist = ["item 1", "item 2", "item 3"]
newlist = ["added item 1", "added item 3"]

for item in mylist:
    for other_item in newlist:
        if other_item.endswith(item):
            print(f"newlist has {item} added")
            break
        
        print(f"newlist still waiting for {item}")

控制台输出是这个

newlist has item 1 added
newlist still waiting for item 2
newlist still waiting for item 2
newlist still waiting for item 3
newlist has item 3 added

如果您不希望第 2 项的输出重复,则可以在第二个 for 循环找到该项目时添加一个变量,如果没有,您可以编写该消息。 就像是:

mylist = ["item 1", "item 2", "item 3"]
newlist = ["added item 1", "added item 3"]

for item in mylist:
    found = False
    for other_item in newlist:
        if other_item.endswith(item):
            print(f"newlist has {item} added")
            found = True
            break

    if found is not True:
        print(f"newlist still waiting for {item}")

这让你:

newlist has item 1 added
newlist still waiting for item 2
newlist has item 3 added

看起来您只需要嵌套循环来检查列表 1 中的每个项目和列表 2 中的每个项目。

mylist = ["item 1", "item 2", "item 3"]
newlist = ["added item 1", "added item 3"]

for item in newlist:
  for item1 in mylist:
    if item1.endswith(item):
        print(f"newlist has {item} added")
    else:
        print(f"newlist still waiting for {item}")

暂无
暂无

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

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