【问题标题】:How to obtain the number of lists(rows) that contain a specific value(string) in at least one of the elements in a row(list)?如何获取行(列表)中至少一个元素中包含特定值(字符串)的列表(行)的数量?
【发布时间】:2020-10-28 16:12:35
【问题描述】:

这个问题很简单。我想知道如何获取包含字符串的列表(行)的数量,该字符串存在于至少一个列表(行)中,其中我的数据以列表列表的形式存储,每个子列表包含字符串。我检查了这个(How to select the rows that contain a specific value in at least one of the elements in a row?),但我的数据不是数据框的形式,我没有足够的声誉点来评论那里。

我的数据如下所示:

[['this', 'is', 'the', 'first', 'document'], ['this', 'document', 'is', 'the', 'second', 'document'], ['and', 'this', 'is', 'the', 'third', 'one'], ['is', 'this', 'the', 'first', 'document']]

【问题讨论】:

    标签: python python-3.x string list


    【解决方案1】:

    鉴于:

    my_list = [['this', 'is', 'the', 'first', 'document'], ['this', 'document', 'is', 'the', 'second', 'document'], ['and', 'this', 'is', 'the', 'third', 'one'], ['is', 'this', 'the', 'first', 'document']]
    

    并且您想搜索 'my_string' 是否存在于您的行中

    那么你可以拥有:

    counter = 0
    
    for row in my_list :
        if 'my_string' in row:
            counter += 1
    print(counter)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-14
      • 2017-09-18
      • 1970-01-01
      • 2022-01-27
      • 2021-09-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多