【问题标题】:How to frame two for loops in list comprehension python如何在列表理解python中构建两个for循环
【发布时间】:2013-09-04 06:40:44
【问题描述】:

我有两个列表如下

tags = [u'man', u'you', u'are', u'awesome']
entries = [[u'man', u'thats'],[ u'right',u'awesome']]

我想从entries 中提取条目,当它们在tags 中时:

result = []

for tag in tags:
    for entry in entries:
        if tag in entry:
            result.extend(entry)

如何将两个循环编写为单行列表理解?

【问题讨论】:

  • 如果您想要一个扁平列表,请使用 itertools.chainlist(chain.from_iterable(entry for tag in tags for entry in entries if tag in entry))

标签: python list for-loop list-comprehension


【解决方案1】:

记住这一点的最好方法是列表推导式中 for 循环的顺序是基于它们在传统循环方法中出现的顺序。最外层循环首先出现,然后是内层循环。

因此,等效的列表推导是:

[entry for tag in tags for entry in entries if tag in entry]

一般来说,if-else 语句出现在第一个 for 循环之前,如果你只有一个 if 语句,它将出现在最后。例如,如果您想添加一个空列表,如果 tag 不在条目中,您可以这样做:

[entry if tag in entry else [] for tag in tags for entry in entries]

【讨论】:

    【解决方案2】:

    应该这样做:

    [entry for tag in tags for entry in entries if tag in entry]
    

    【讨论】:

      【解决方案3】:

      适当的信用证是

      [entry for tag in tags for entry in entries if tag in entry]
      

      LC 中的循环顺序与嵌套循环类似,if 语句在末尾,条件表达式在开头,类似于

      [a if a else b for a in sequence]
      

      查看演示 -

      >>> tags = [u'man', u'you', u'are', u'awesome']
      >>> entries = [[u'man', u'thats'],[ u'right',u'awesome']]
      >>> [entry for tag in tags for entry in entries if tag in entry]
      [[u'man', u'thats'], [u'right', u'awesome']]
      >>> result = []
          for tag in tags:
              for entry in entries:
                  if tag in entry:
                      result.append(entry)
      
      
      >>> result
      [[u'man', u'thats'], [u'right', u'awesome']]
      

      编辑 - 由于您需要将结果展平,因此您可以使用类似的列表推导然后展平结果。

      >>> result = [entry for tag in tags for entry in entries if tag in entry]
      >>> from itertools import chain
      >>> list(chain.from_iterable(result))
      [u'man', u'thats', u'right', u'awesome']
      

      把这个加起来就可以了

      >>> list(chain.from_iterable(entry for tag in tags for entry in entries if tag in entry))
      [u'man', u'thats', u'right', u'awesome']
      

      您在这里使用生成器表达式而不是列表推导式。 (也完全符合 79 个字符的限制(没有 list 调用))

      【讨论】:

        【解决方案4】:

        在理解中,嵌套列表迭代应该遵循与等效的叠层 for 循环相同的顺序。

        为了理解,我们将从 NLP 中举一个简单的例子。您想从句子列表中创建所有单词的列表,其中每个句子都是单词列表。

        >>> list_of_sentences = [['The','cat','chases', 'the', 'mouse','.'],['The','dog','barks','.']]
        >>> all_words = [word for sentence in list_of_sentences for word in sentence]
        >>> all_words
        ['The', 'cat', 'chases', 'the', 'mouse', '.', 'The', 'dog', 'barks', '.']
        

        要删除重复的单词,可以使用集合 {} 而不是列表 []

        >>> all_unique_words = list({word for sentence in list_of_sentences for word in sentence}]
        >>> all_unique_words
        ['.', 'dog', 'the', 'chase', 'barks', 'mouse', 'The', 'cat']
        

        或申请list(set(all_words))

        >>> all_unique_words = list(set(all_words))
        ['.', 'dog', 'the', 'chases', 'barks', 'mouse', 'The', 'cat']
        

        【讨论】:

          【解决方案5】:
          tags = [u'man', u'you', u'are', u'awesome']
          entries = [[u'man', u'thats'],[ u'right',u'awesome']]
          
          result = []
          [result.extend(entry) for tag in tags for entry in entries if tag in entry]
          
          print(result)
          

          输出:

          ['man', 'thats', 'right', 'awesome']
          

          【讨论】:

            【解决方案6】:
            return=[entry for tag in tags for entry in entries if tag in entry for entry in entry]
            

            【讨论】:

            • 您好,欢迎来到 Stack Overflow!请发布解释,而不仅仅是代码。
            • 您好!虽然这段代码可以解决问题,including an explanation 解决问题的方式和原因确实有助于提高帖子的质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提问的人。请edit您的回答添加解释并说明适用的限制和假设。
            猜你喜欢
            • 1970-01-01
            • 2021-08-17
            • 1970-01-01
            • 2023-03-11
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2010-10-01
            相关资源
            最近更新 更多