给定list或string的参数,我想在不使用len()函数的情况下返回长度,除了迭代之外,还有更好的方法吗?
def getLen(myType):
for index,value in enumerate(myType):
continue
return index + 1
答案 0 :(得分:2)
这是作弊行为,但开头的要求相当愚蠢:
myType.__len__()
答案 1 :(得分:0)
>>> sum(1 for x in "thisissilly")
11
>>> sum(map(lambda i:1, "anotheronefortheroad"))
20
>>> sum(1 for x in [3,78,76,34,78])
5
>>> sum(map(lambda i:1, ['betty','bop','doing','the','do']))
5