Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在没有内置函数或计数器的情况下使用python确定字符串中字母的出现次数_Python_Python 3.x - Fatal编程技术网

如何在没有内置函数或计数器的情况下使用python确定字符串中字母的出现次数

如何在没有内置函数或计数器的情况下使用python确定字符串中字母的出现次数,python,python-3.x,Python,Python 3.x,我对使用ord和id函数来获取字符串中字母的值有一个粗略的想法,但不知道如何为相同值的其他类似事件递增。这是我面试的个人准备。请给我想法和建议,无需代码。用户一个计数器变量,类似这样的内容应该可以让您开始: >>> count = 0 >>> meaty = 'asjhdkajhskjfhalksjhdflaksjdkhaskjd' >>> for i in meaty: ... if i == 'a': ... count+=1

我对使用ord和id函数来获取字符串中字母的值有一个粗略的想法,但不知道如何为相同值的其他类似事件递增。这是我面试的个人准备。请给我想法和建议,无需代码。

用户一个计数器变量,类似这样的内容应该可以让您开始:

>>> count = 0
>>> meaty = 'asjhdkajhskjfhalksjhdflaksjdkhaskjd'
>>> for i in meaty:
...   if i == 'a':
...     count+=1
...
>>> print count
5
如果要跟踪多个字母的多次出现,请使用该字母作为存储计数器整数的字典键:

>>> count = {}
>>> for i in meaty:
...   key = i
...   if key in count:
...     count[key]+=1
...   else:
...     count[key]=1
...
>>> count
{'a': 5, 'd': 4, 'f': 2, 'h': 5, 'k': 6, 'j': 6, 'l': 2, 's': 5}

编辑:Meh,没有计数器,没有内置函数,没有堆栈溢出编码示例?不知道如何在不计数的情况下计数,python本质上不是内置函数的集合吗?堆栈溢出不是真正的编码帮助吗

如果不使用内置函数或计数器,我会跳出框框思考。只需在他们的办公桌上放一条注释,指导用户实际做什么。这使用了足够的Python来完成任务,而不使用任何内置函数

"""
Using a pencil, write down the word, then starting at the first letter:
Write down the letter and put a dash or tick or something next to it
Look for that letter in the word, and add additional marks next to the letter to keep track of the count.
When you've reached the end of the word, go to the next letter and repeat the process 
Skip any letters that you have already counted(Otherwise instead of the count of each letter's occurrences, you'll get the factorial of the count!)
"""

字符串中出现的所有字母都不会在字符串中循环时创建字典?假设你们是一台计算机,并在纸上用伪代码进行操作。然后把它翻译成Python,把算法放到解释器中,然后进行测试。我不太理解这里的限制。没有功能?没有柜台吗?扔掉dict的想法,因为这些值只是递增的整数,对吗?那不是柜台吗?另外,最好不要使用in关键字,因为它调用_包含一个函数。此外,您还必须澄清计数器的含义,因为如果不以某种方式计算,很难确定某个东西出现了多少次。@NaeiKinDus-公平地说,我不知道如何在不进行某种计数的情况下确定某个任意对象的数量,无论是num+=1、collections.Counter还是len。@TigerhawkT3我完全同意,这就是为什么我没有给出答案;这只不过是在同一页的作品。不妨做1。计算每个字母出现的次数。。如果这是一个骗人的问题,我会感到惊讶,但如果是,就不应该这样。