简体   繁体   中英

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

Then you can have:

counter = 0

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM