a_list.count(a_callable)? [英] a_list.count(a_callable) ?

查看:48
本文介绍了a_list.count(a_callable)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我想知道扩展列表的count()方法是否有用

来接受一个可调用的对象?它的作用应该是非常直观的:

计算可调用项返回True或其他任何物品的数量

逻辑等效(非空序列,非零数字等) )。


这将返回与len(filter(a_callable,a_list)),

相同的结果,但不构造丢弃的中间列表< len()完成后



这也相当于

n = 0

for a in a_list:

如果a_callable(i):n + = 1

但是代码更短更容易阅读。它也会更快地运行




这是我的第一篇文章,如果我没有在
$中发布,请耐心等待。 b $ b正确的方式。


问候,

Ping

解决方案

6月14日下午2:53,Ping< ping.nsr .... @ gmail.comwrote:





我想知道扩展列表的count()方法是否有用

来接受一个可调用的对象?它的作用应该是非常直观的:

计算可调用项返回True或其他任何物品的数量

逻辑等效(非空序列,非零数字等) )。


这将返回与len相同的结果(filter(a_callable,a_list)),



map and filter在列表介绍后基本上已经过时

comprehensions;你的表达式相当于:

len([i for a in a_list if a_callable(i)])


然后可以将其转换为生成器表达式(回合

括号而不是方括号)以避免中间列表:

len((如果a_callable(i),我在a_list中为i))


或语法等价(避免lispy括号):

len(如果a_callable(i),我在a_list中为i)


但是没有构建一个被丢弃的中间列表
$ len()完成后的b $ b。


这也等同于

n = 0
$ a b $ b我在a_list中:

如果a_callable(i):n + = 1

但是要短得多更易于阅读的代码。它也会更快地运行




这是我的第一篇文章,如果我没有在
$中发布,请耐心等待。 b $ b正确的方式。


问候,

Ping



6月14日下午3:37,Dustan< DustanGro ... @ gmail.comwrote:


地图和过滤器在引入后基本上已过时list

comprehensions



值得注意的是,列表推导不需要

你写了一个新的功能;他们采取适当的表达方式。

。有关列表推导的更多信息,请参阅:

http://docs.python.org/tut/node7.htm...00000000000000


生成器表达式是相同的,除了语法上它们有

圆形括号而不是方形,它们返回一个生成器而不是列表的
,这允许进行惰性评估。


6月14日下午3:37,Dustan< DustanGro ... @ gmail.comwrote:


然后可以将其转换为生成器表达式(圆形

括号而不是方括号)以避免中间列表:

len((如果a_callable(i),我在a_list中为i))



对不起,每个人都有过多的帖子。


我刚刚意识到生成器表达式不起作用。我不是

确定如何有效地实现其他方式并且不会因为前面的例子显示累积计数而没有使用

内存。


Hi,

I''m wondering if it is useful to extend the count() method of a list
to accept a callable object? What it does should be quite intuitive:
count the number of items that the callable returns True or anything
logically equivalent (non-empty sequence, non-zero number, etc).

This would return the same result as len(filter(a_callable, a_list)),
but without constructing an intermediate list which is thrown away
after len() is done.

This would also be equivalent to
n = 0
for i in a_list:
if a_callable(i): n += 1
but with much shorter and easier-to-read code. It would also run
faster.

This is my first post and please bear with me if I''m not posting it in
the right way.

Regards,
Ping

解决方案

On Jun 14, 2:53 pm, Ping <ping.nsr....@gmail.comwrote:

Hi,

I''m wondering if it is useful to extend the count() method of a list
to accept a callable object? What it does should be quite intuitive:
count the number of items that the callable returns True or anything
logically equivalent (non-empty sequence, non-zero number, etc).

This would return the same result as len(filter(a_callable, a_list)),

map and filter are basically obsolete after the introduction of list
comprehensions; your expression is equivalent to:
len([i for i in a_list if a_callable(i)])

Which can then be converted into a generator expression (round
brackets instead of square brackets) to avoid the intermediate list:
len((i for i in a_list if a_callable(i)))

Or syntactically equivalent (avoiding lispy brackets):
len(i for i in a_list if a_callable(i))

but without constructing an intermediate list which is thrown away
after len() is done.

This would also be equivalent to
n = 0
for i in a_list:
if a_callable(i): n += 1
but with much shorter and easier-to-read code. It would also run
faster.

This is my first post and please bear with me if I''m not posting it in
the right way.

Regards,
Ping



On Jun 14, 3:37 pm, Dustan <DustanGro...@gmail.comwrote:

map and filter are basically obsolete after the introduction of list
comprehensions

It is probably worth noting that list comprehensions do not require
that you write a new function; they take any expression where
appropriate. For more information on list comprehensions, see:

http://docs.python.org/tut/node7.htm...00000000000000

Generator expressions are the same, except syntactically they have
round brackets instead of square, and they return a generator instead
of a list, which allows for lazy evaluation.


On Jun 14, 3:37 pm, Dustan <DustanGro...@gmail.comwrote:

Which can then be converted into a generator expression (round
brackets instead of square brackets) to avoid the intermediate list:
len((i for i in a_list if a_callable(i)))

Sorry for the excess of posts everybody.

I just realized that the generator expression would not work. I''m not
sure how else could be implemented efficiently and without using up
memory besides by accumulating the count as your earlier example shows.


这篇关于a_list.count(a_callable)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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