![](/img/trans.png)
[英]How to select the rows that contain a specific value in at least one of the elements in a row?
[英]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)?
这个问题很简单。 我想知道如何获取包含字符串的列表(行)的数量,该字符串存在于至少一个列表(行)中,其中我的数据以列表列表的形式存储,每个子列表包含字符串。 我检查了这个( 如何选择至少在一行中的一个元素中包含特定值的行? )但我的数据不是数据框的形式,我没有足够的信誉点来评论在那里。
我的数据看起来像这样:
[['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']]
并且您想搜索行中是否存在'my_string'
然后你可以有:
counter = 0
for row in my_list :
if 'my_string' in row:
counter += 1
print(counter)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.