Python 没有 len 函数的字符串长度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3992192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
String length without len function
提问by sam
Can anyone tell me how can I get the length of a string without using the len()function or any string methods. Please anyone tell me as I'm tapping my head madly for the answer.
Thank you.
谁能告诉我如何在不使用len()函数或任何字符串方法的情况下获取字符串的长度。请任何人告诉我,因为我正在疯狂地敲着头寻找答案。
谢谢你。
回答by g.d.d.c
Why you need to avoid the len function is beyond me, but strings are iterables. You should be able to do this:
为什么需要避免 len 函数超出了我的理解,但字符串是可迭代的。你应该能够做到这一点:
strlen = 0
for c in myString:
strlen += 1
回答by gtrak
easy:
简单:
length=0
for x in "This is a string":
length+=1
print(length)
回答by John La Rooy
>>> sum(map(lambda x:1, "hello world"))
11
>>> sum(1 for x in "foobar")
6
>>> from itertools import count
>>> zip(count(1), "baz")[-1][0]
3
A "tongue twister"
一个“绕口令”
>>> sum(not out not in out for out in "shake it all about")
18
some recursive solutions
一些递归解决方案
>>> def get_string_length(s):
... return 1 + get_string_length(s[1:]) if s else 0
...
>>> get_string_length("hello world")
11
>>> def get_string_length_gen(s):
... yield 1 + next(get_string_length_gen(s[1:])) if s else 0
...
>>> next(get_string_length_gen("hello world"))
11
>>>
回答by Ken
It's a weird question so here's a weird answer!
这是一个奇怪的问题,所以这里有一个奇怪的答案!
try:
for i in itertools.count(): mystring[i]
except IndexError:
pass
回答by ghostdog74
>>> import re
>>> s
'mylongstring'
>>> re.subn(".","1",s)[-1]
12
>>>
If string contains new lines
如果字符串包含新行
>>> s="mys\ntring\n"
>>> re.compile(".",re.DOTALL).subn("",s)[-1]
10
回答by Nate
Not very efficient but very concise:
效率不高但非常简洁:
def string_length(s):
if s == '': return 0
return 1 + string_length(s[1:])
回答by Gabe
Here's an O(1) method:
这是一个 O(1) 方法:
def strlen(s):
if s == "": return 0
return s.rindex(s[-1]) + 1
In other words, it doesn't work by counting the characters, so should be just as fast for a 1GB string as it is for a 1 byte string.
换句话说,它不能通过计算字符来工作,因此对于 1GB 字符串应该与对于 1 字节字符串一样快。
It works by looking at the last character and searching from the very end to find that character. Since it's the last character it will always find it at the first place it looks, essentially always returning the index of the last character. The length is just one more than the index of the last character.
它的工作原理是查看最后一个字符并从最后开始搜索以找到该字符。因为它是最后一个字符,所以它总是会在它看起来的第一个位置找到它,基本上总是返回最后一个字符的索引。长度仅比最后一个字符的索引多一个。
回答by Gabe
Here's a way to do it by counting the number of occurences of the empty string within the string:
这是一种通过计算字符串中空字符串出现的次数来实现的方法:
def strlen(s):
return s.count('') - 1
Since "".count("")returns 1, you have to subtract 1 to get the string's length.
由于"".count("")返回 1,您必须减去 1 才能获得字符串的长度。
回答by PaulMcG
Make a file-like object from the string, read the entire object, then tell your offset:
从字符串中创建一个类似文件的对象,读取整个对象,然后告诉您的偏移量:
>>> import StringIO
>>> ss = StringIO.StringIO("ABCDEFGHIJ")
>>> ss.read()
'ABCDEFGHIJ'
>>> ss.tell()
10
回答by Lioste
You can get the length of the string with a forloop.
您可以使用for循环获取字符串的长度。
For example, instead of:
例如,而不是:
string=input("Enter a string")
print(len(string))
Do this:
做这个:
string=input("Enter a string")
a=0
for letter in string:
a=a+1
print(a)

