擅长:python、mysql、java
<p>根据您最近的评论,您似乎希望计算包含第一个列表元素的列表的数量。下面是一个小函数,它可以做到这一点:</p>
<pre class="lang-py prettyprint-override"><code>def count_in_lists(e, lol):
"""
Count how many of the lists passed in the list of lists 'lol' contain
the element 'e'.
"""
count = 0
for current_list in lol:
if e in current_list:
count += 1
return count
</code></pre>
<p>现在,与casraf的答案类似,遍历<code>l1</code>,并调用函数<code>count_in_lists()</code>,其中当前元素{<cd1>}作为第一个参数,包含您感兴趣的所有其他列表的列表作为第二个参数:</p>
^{pr2}$
<p>这将为您提供以下输出:</p>
<pre><code>'fish' is contained in 1 other lists
'boat' is contained in 0 other lists
'oar' is contained in 0 other lists
</code></pre>