简体   繁体   English

如何在不使用计数器或python中的字典的情况下查找项目出现在字符串列表中的次数?

[英]How to find the number of times an item appears in a list of list of strings without using counter, or dictionary in python?

If I have如果我有

animal = [['cat', 'cat', 'dog'], ['cat', 'cat', 'dog'], ['cat', 'cat', 'dog']] 

and

item_to_find = ['cat', 'dog']

how do I calculate the number of times each string inside item_to_find can be found in animal ?如何计算item_to_find中的每个字符串可以在animal中找到的次数? for example, for 'cat' , the answer would be 6, and for 'dog' it would be 3. I need to do this without using collectiouns.Counter or a dictionary.例如,对于'cat' ,答案是 6,对于'dog' ,答案是 3。我需要在不使用collectiouns.Counter或字典的情况下执行此操作。 I tried to use count , but it only works if it's a list of strings, and not a list of list of strings.我尝试使用count ,但它仅适用于字符串列表,而不是字符串列表。

I tried to do it this way:我试着这样做:

for i in item_to_find:
    print(animal.count(i))

but like I was saying this only works for a list of strings and not a list of list of strings.但就像我说的那样,这只适用于字符串列表,而不适用于字符串列表。

My result would look like something like this: [6, 3] (as a list).我的结果看起来像这样: [6, 3] (作为列表)。

Use sum along with count to sum up the counts in all the sub-lists:使用sumcount来总结所有子列表中的计数:

>>> animal = [['cat', 'cat', 'dog'], ['cat', 'cat', 'dog'], ['cat', 'cat', 'dog']]
>>> item_to_find = ['cat', 'dog']
>>> [(item, sum(a.count(item) for a in animal)) for item in item_to_find]
[('cat', 6), ('dog', 3)]

If you wanted to avoid using count you can do it almost as easily by using a nested generator with sum :如果您想避免使用count ,您可以通过使用带有sum的嵌套生成器来轻松地做到这一点:

>>> [(item, sum(i == item for a in animal for i in a)) for item in item_to_find]
[('cat', 6), ('dog', 3)]

If you don't want to know which item each count goes with, make it a list of just the counts instead of a tuple that includes the item:如果您不想知道每个计数与哪个项目相关,请将其设为仅包含计数的列表,而不是包含该项目的元组:

>>> [sum(a.count(item) for a in animal) for item in item_to_find]
[6, 3]

You can use iteration:您可以使用迭代:

occurrences = [0] * len(item_to_find)
for sublist in animal:
    for idx, item in enumerate(item_to_find):
        occurrences[idx] += sublist.count(item)

Now occurrences will be [6, 3] for the example given.对于给出的示例,现在出现的次数将是 [6, 3]。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何查找列表中最常见项目出现的最大次数? - How to find the maximum number of times that the most common item in a list appears? 尝试查找项目出现在另一个字符串列表中的次数 - Try to find the number of times an item appears in another list of list of string 如何使用.count计算一个列表中每个项目出现在python中另一个列表中的次数? - How to use .count to calculate number of times each item in one list appears in another list in python? Python 3使用单词计数器列出行和单词从文件数组中出现的次数 - Python 3 using word counter to list line & how many times a word appears from an array in file Python:如何使用显示特定次数的python列表中的项目? - Python: How to use an item from a python list which appears a specific number of times? 如何在不计算负数的情况下找到一个数字在列表中出现的次数? - How can I find how many times a number appears in a list without counting negatives? 计算一系列字符串在列表中出现的次数 - Python - Count how many times a series of strings appears in a list - Python 如何通过Python删除在列表中多次出现的项目? - How to remove an item that appears multiple times in a list by Python? 如何打印元素出现在Python列表中的次数? - How to print the number of times an element appears in a list in Python? 如何使用预定义列表中的单词出现在数据框的文本列中的次数的计数器创建新列? - How to make a new column with counter of the number of times a word from a predefined list appears in a text column of the dataframe?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM