Python 如何获取在一行(列表)中至少一个元素中包含特定值(字符串)的列表(行)数?

Python 如何获取在一行(列表)中至少一个元素中包含特定值(字符串)的列表(行)数?,python,python-3.x,string,list,Python,Python 3.x,String,List,问题很简单。我想知道如何获取包含字符串的列表(行)的数量,该字符串至少存在于其中一个列表(行)中,其中我的数据以列表的形式存储,每个子列表包含字符串。我签出了这个()但是我的数据不是以数据框的形式出现的,我没有足够的信誉点在那里发表评论 我的数据如下所示: [['this', 'is', 'the', 'first', 'document'], ['this', 'document', 'is', 'the', 'second', 'document'], ['and', 'this', 'is

问题很简单。我想知道如何获取包含字符串的列表(行)的数量,该字符串至少存在于其中一个列表(行)中,其中我的数据以列表的形式存储,每个子列表包含字符串。我签出了这个()但是我的数据不是以数据框的形式出现的,我没有足够的信誉点在那里发表评论

我的数据如下所示:

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

鉴于此:

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

for row in my_list :
    if 'my_string' in row:
        counter += 1
print(counter)
并且您希望搜索您的行中是否存在
“我的字符串”

然后您可以拥有:

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

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