[英]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)?
The question is pretty straightforward .这个问题很简单。 I would like to know how to obtain the number of lists(rows) that contain a string which exists in at least one of the lists(rows) where my data is stored in the form of a list of lists with each sub-list containing strings.
我想知道如何获取包含字符串的列表(行)的数量,该字符串存在于至少一个列表(行)中,其中我的数据以列表列表的形式存储,每个子列表包含字符串。 I checked this( How to select the rows that contain a specific value in at least one of the elements in a row? ) out but my data is not in the form of a data frame and I don't have enough reputation points to comment on there.
我检查了这个( 如何选择至少在一行中的一个元素中包含特定值的行? )但我的数据不是数据框的形式,我没有足够的信誉点来评论在那里。
My data looks something like this:我的数据看起来像这样:
[['this', 'is', 'the', 'first', 'document'], ['this', 'document', 'is', 'the', 'second', 'document'], ['and', 'this', 'is', 'the', 'third', 'one'], ['is', 'this', 'the', 'first', 'document']]
Given that:鉴于:
my_list = [['this', 'is', 'the', 'first', 'document'], ['this', 'document', 'is', 'the', 'second', 'document'], ['and', 'this', 'is', 'the', 'third', 'one'], ['is', 'this', 'the', 'first', 'document']]
And you want to search if 'my_string'
exists in your rows并且您想搜索行中是否存在
'my_string'
Then you can have:然后你可以有:
counter = 0
for row in my_list :
if 'my_string' in row:
counter += 1
print(counter)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.