计算嵌套列表中包含特定元素的列表数 [英] Count the number of lists containing specific element in a nested list

查看:544
本文介绍了计算嵌套列表中包含特定元素的列表数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表,例如:

  res = [['a','b','a' ],['a','b','c'],['a']] 

我想计算有多少列表包含特定的字母。例如,'a'包含在3个列表中,'b'包含在2个列表中,'c'包含在1个列表中。



我到目前为止:

  count = 0 
docs ='a'

list1 = []

在范围(len(res))中的c:
for i in res [0]:
list1.append(i)
for i在list1中:
如果i == docs:
count = 1
打印计数


解决方案

当你发现自己说我想计算多少...时,有一个很好的机会 Counter() / code>模块,可以帮助。



在这种情况下,我们要计算每个字母出现的列表数。因为我们不想计数任何字母对于每个子列表不止一次,我们会将其转换为

 >>> res = [['a','b','a'],['a','b','c'],['a']] 
>> [set(x)for x in res]
[{'b','a'},{'c','b','a'},{'a'}]

订单混乱了,但这并不重要,只要每个列表只有一个字母。 / p>

现在我们要将这些字母组合成一个序列,所以我们可以对它们进行计数。我们可以这样做:

 >>> [s for x in res for s in set(x)] 
['b','a','c','b','a','a']

...但这有点难以遵循。幸运的是,在 itertools 模块中有一个函数 chain() ,这样做更容易阅读。我们希望 chain.from_iterable()版本:

 > ;> from itertools import chain 
>>>> c = chain.from_iterable(set(x)for x in res)
>>> list(c)
['b','a','c','b','a','a']

不要担心列表(c)太多 - chain()返回迭代器,这意味着没有任何计算,直到我们实际上做的事情与结果(如使它成一个列表),所以我做了,以显示它产生的。



无论如何,我们现在需要做的是pass该顺序为 Counter()

 > from collections import Counter 
>>>>计数器(chain.from_iterable(set(x)for x in res))
计数器({'a':3,'b':2,'c':1})

这是整个事情:

  from collections import Counter 
from itertools import chain

res = [['a','b','a'],['a','b','c'] ,['a']]

letter_count = Counter(chain.from_iterable(set(x)for x in res))

print(letter_count ['a']) #prints 3


I have a list, for example:

res = [['a', 'b', 'a'], ['a', 'b', 'c'], ['a']]

I want to count how many lists contains a specific letter. For instance, 'a' is contained in 3 lists, 'b' is contained in 2 lists and 'c' is contained in 1 list.

The code below is what I have so far:

count=0
docs='a'

list1=[]

for c in range(len(res)):
    for i in res[0]:
        list1.append(i)
        for i in list1:
            if i == docs:
                count=1
print count

解决方案

When you find yourself saying "I want to count how many ...", there's a good chance Counter(), from the collections module, can help.

In this case, we want to count how many lists each letter occurs in. Since we don't want to count any letter more than once for each sublist, we'll convert them to sets:

>>> res = [['a', 'b', 'a'], ['a', 'b', 'c'], ['a']]
>>> [set(x) for x in res]
[{'b', 'a'}, {'c', 'b', 'a'}, {'a'}]

The order gets mixed up, but that doesn't matter, as long as we only have one letter from each list.

Now we want to join those sets of letters into one sequence, so we can count them all. We could do it like this:

>>> [s for x in res for s in set(x)]
['b', 'a', 'c', 'b', 'a', 'a']

... but that's a little hard to follow. Luckily there's a function in the itertools module called chain() that does the same thing and is a little easier to read. We want the chain.from_iterable() version:

>>> from itertools import chain
>>> c = chain.from_iterable(set(x) for x in res)
>>> list(c)
['b', 'a', 'c', 'b', 'a', 'a']

Don't worry about that list(c) too much - chain() returns an iterator, which means nothing gets calculated until we actually do something with the result (like make it into a list), so I did that to show what it produces.

Anyway, all we need to do now is pass that sequence to Counter():

>>> from collections import Counter
>>> Counter(chain.from_iterable(set(x) for x in res))
Counter({'a': 3, 'b': 2, 'c': 1})

Here's the whole thing:

from collections import Counter
from itertools import chain

res = [['a', 'b', 'a'], ['a', 'b', 'c'], ['a']]

letter_count = Counter(chain.from_iterable(set(x) for x in res))

print(letter_count['a'])  # prints 3

这篇关于计算嵌套列表中包含特定元素的列表数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆