计算物品 [英] counting items

查看:66
本文介绍了计算物品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我放弃了。


计算清单中物品数量的最佳方法是什么?


For例如,


a = [[1,2,4],4,5,[2,3]]


我想知道一个项目中有多少项(答案应该是7 - 我不要这么多b $ b想要它是4)


我试过:


b = len([x代表y代表y中的y])


这不起作用因为你会得到非迭代-sequence。


我试过:


g = lambda x:(1,len(x))[isinstance(x,(list, tuple,dict))

b = sum(x中的x为lambda(x))


并且因为我得到一个TypeError而无法正常工作来自len功能(不要
知道原因)


我当然可以:


i = 0

for x in a:

if isinstance(x,(list,tuple,dict)):

i + = len(x )

否则:

i + = 1


但是那就像C一样......


谢谢,

Okay, I give up.

What''s the best way to count number of items in a list?

For instance,

a=[[1,2,4],4,5,[2,3]]

I want to know how many items are there in a (answer should be 7 - I don''t
want it to be 4)

I tried:

b=len([x for y in a for x in y])

That doesn''t work because you would get an iteration over non-sequence.

I tried:

g=lambda x: (1,len(x))[isinstance(x,(list,tuple,dict))]
b=sum(lambda(x) for x in a)

and that didn''t work because I get a TypeError from the len function (don''t
know why)

I can, of course:

i=0
for x in a:
if isinstance(x,(list,tuple,dict)):
i += len(x)
else:
i += 1

but that''s so C-like...

Thanks,

推荐答案

"这是我 <它*** @ yahoo.com>在消息中写道

新闻:uk ***************** @newssvr21.news.prodigy.co m ...
"It''s me" <it***@yahoo.com> wrote in message
news:uk*****************@newssvr21.news.prodigy.co m...
计算列表中项目数量的最佳方法是什么?

例如,

a = [[1,2,4] ,4,5,[2,3]]

我想知道有多少项目(答案应该是7 - 我不希望它是4 )
What''s the best way to count number of items in a list?

For instance,

a=[[1,2,4],4,5,[2,3]]

I want to know how many items are there in a (answer should be 7 - I don''t
want it to be 4)




怎么样?


def totallen(x):

if isinstance( x,(list,tuple,dict)):

返回总和(地图(totallen,x))

返回1



How about this?

def totallen(x):
if isinstance(x, (list, tuple, dict)):
return sum(map(totallen, x))
return 1


这是我 <它*** @ yahoo.com>在消息中写道

news:uk ***************** @ newssvr21.news.prodigy.co m ...
"It''s me" <it***@yahoo.com> wrote in message
news:uk*****************@newssvr21.news.prodigy.co m...
好的,我放弃了。

计算清单中项目数量的最佳方法是什么?

例如,

a = [[1,2,4],4,5,[2,3]]

我想知道有多少项目(答案应该是7 - 我不是'' t
希望它是4)
Okay, I give up.

What''s the best way to count number of items in a list?

For instance,

a=[[1,2,4],4,5,[2,3]]

I want to know how many items are there in a (answer should be 7 - I don''t
want it to be 4)



< snip>


我肯定见过很多关于列表的扁平化。我发现

这个版本的flatten在某个地方,我以为我是从Python获得它的
Cookbook但我现在找不到它。也许它是在c.l.py上发布的。我不是* b $ b *不要声称作者身份,我只是这种看起来很干净的

解决方案的崇拜者。


def flatten(a):

如果不是isinstance(a,(元组,列表)):如果len(a)== 0,则返回[a]

:return []

返回flatten(a [0])+ flatten(a [1:])


a = [[1,2,4] ,4,5,[2,3],6,[6],[],''askjdf'']


print len(flatten(a))


给出的价值为10.


考虑到这种情况经常发生,可能会有某种地方出现某种情况

压扁()例程在std dist中,也许是itertools?


- Paul


<snip>

I''ve sure seen a lot of questions about the flattening of lists. I found
this version of flatten somewhere, I thought I got it from the Python
Cookbook but I can''t find it now. Perhaps it was posted here on c.l.py. I
*don''t* claim authorship, I''m merely an admirer of such a clean-looking
solution.

def flatten(a):
if not isinstance(a, (tuple,list)): return [a]
if len(a)==0: return []
return flatten(a[0])+flatten(a[1:])

a = [[1, 2, 4], 4, 5, [2, 3], 6, [6], [], ''askjdf'']

print len(flatten(a))

gives the value 10.

Considering how often this comes up, might there be a place for some sort of
flatten() routine in the std dist, perhaps itertools?

-- Paul


这是我写的:
It''s me wrote:
好的,我放弃了。

计算列表中可能包含列表的项目数量的最佳方法是什么?
Okay, I give up.

What''s the best way to count number of items in a list [that may contain lists]?



a = [[1,2,4],4,5,[2,3]]


def iterall(seq):

for seq中的项目:

尝试:

for subitem in iterall(item):

收益子项

除TypeError:

收益项


all = [x for ite inl(a)]

print len(all)


a = [[1,2,4],4,5,[2,3]]

def iterall(seq):
for item in seq:
try:
for subitem in iterall(item):
yield subitem
except TypeError:
yield item

all = [x for x in iterall(a)]
print len(all)


这篇关于计算物品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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