【问题标题】:How to compare elements in lists and check if first list element contains in another list's element using python如何使用python比较列表中的元素并检查第一个列表元素是否包含在另一个列表的元素中
【发布时间】:2021-12-13 15:24:28
【问题描述】:

我有两个列表 l1l2 包含类似的元素

l1 = ['a','b','c','d']
l2 = ['a is greater', 'f is greater', 'c is greater']

我想比较 l1 的元素并查找 l2 是否包含在它们的元素中。 期望的输出是

f is greater

按照我试过的代码,

for i in l2:
    for j in l2:
    if j not in l1:
        print(i)

但我观察到的输出是什么

a is greater
a is greater
f is greater
f is greater
c is greater
c is greater

请帮助我了解我需要添加什么以获得适当的输出。谢谢。

【问题讨论】:

    标签: python python-3.x list python-2.7 arraylist


    【解决方案1】:

    用途:

    l1 = ['a','b','c','d']
    l2 = ['a is greater', 'f is greater', 'c is greater']
    
    # iterate over the elements of l2
    for i in l2:
    
        # check if the first letter of e is in l1
        if i[0] not in l1:
            print(i)
    

    输出

    f is greater
    

    您无需对l2 的元素进行两次迭代,并使用in 检查值(i[0])是否在集合(l1)中。

    更新

    如果您想检查不同的位置,只需更改 i 上的索引,例如,如果您想检查最后一个位置:

    l1 = ['a','b','c','d']
    l2 = ['Greater is c', 'Greater is f', 'Greater is d']
    
    # iterate over the elements of l2
    for i in l2:
    
        # check if the first letter of e is in l1
        if i[-1] not in l1:  # note the -1
            print(i)
    

    输出

    Greater is f
    

    如果您想考虑l1 中不存在句子者的所有单词(由空格分隔)的位置,一种方法:

    l1 = ['a', 'b', 'c', 'd']
    l2 = ['Greater is c', 'Greater is f', 'f is greater', "Hello a cat"]
    
    s1 = set(l1)
    
    # iterate over the elements of l2
    for i in l2:
    
        # check if the first letter of e is in l1
        if s1.isdisjoint(i.split()):
            print(i) 
    

    输出

    Greater is f
    f is greater
    

    如果检查字符串是否包含,请执行以下操作:

    l1 = ['Book Date:', 'Statement Number:', 'Opening Book Balance:', 'Closing Book Balance:', 'Number Of Debits:',
          'Number of Credits:', 'Total Debits:', 'Total Credits:', 'Report Date:', 'Created by:', 'Modified by:', 'Printed by:']
    l2 = ['<p>Book Date: 06-01-21 To 06-30-21</p>', '<p>Statement Number: 126 </p>', '<p>this value need to print</p>']
    
    
    # iterate over the elements of l2
    for i in l2:
    
        # check if the first letter of e is in l1
        if not any(j in i for j in l1):
            print(i)
    

    输出

    <p>this value need to print</p>
    

    【讨论】:

    【解决方案2】:

    给你:

    ref = ('a','b','c','d')
    l2 = ['a is greater', 'f is greater', 'c is greater']
    l3 = ['Greater is c', 'Greater is f', 'Greater is d']
    l4 = ['...c...', '...f...', '...d...']
    
    [item for item in l2 if not item.startswith(ref)] # 'f is greater'
    [item for item in l3 if not item.endswith(ref)]   # 'Greater is f'
    [item for item in l4 if not any(letter in item for letter in ref)] # '...f...'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-01
      • 1970-01-01
      • 2021-08-24
      • 2022-11-14
      • 2021-03-02
      • 1970-01-01
      • 2022-01-06
      • 2020-07-07
      相关资源
      最近更新 更多