简体   繁体   English

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

[英]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.

相关问题 如何选择行中至少一个元素中包含特定值的行? - How to select the rows that contain a specific value in at least one of the elements in a row? 从多个列表中选择随机数,它必须至少包含每个列表中的一个数字 - Choose random numbers from multiple lists and it must contain at least one number from each list 如何获取特定行号处的日期时间索引值 - How to obtain the datetime index value at a specific row number 在 dataframe 中查找必须包含列表中至少 2 个元素的行 - Find rows in dataframe that must contain at least 2 elements from a list 用列表中的至少一个值过滤 DataFrame 行 - Filter DataFrame rows with at least one value in list 从列表列表中删除包含特定字符串的列表 - Removing lists that contain specific string from list of lists 如何检查列表中特定元素的字符串值? - How to check specific elements of a list for a string value? 如何在Python映射的值中比较列表中的元素,并检查是否至少有n个元素匹配? - How to compare elements of a list in a Python map's value, and check to see if at least n number of elements match? 按元素中的特定值将python列表拆分为列表 - Split python list into lists by a specific value in elements 如果其中的一个单词在Python中不包含某些字符,如何从列表列表中删除一行? - How to remove a row from a list of lists if one of the words in it does not contain certain characters in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM