如何计算python中给定名称(字符串)中每个字符的出现次数,并在没有dict的情况下以正常格式显示



假设我的输入是:

marcel bentok tanaka

输出应为

m-1,a-4,r-1,c-1,l-1,e-2,l-1,b-1,n-2,t-2,o-1,k-2

我不想计算空格,也不想使用dict或导入函数。如何以最简单的方式实现这一点?请帮忙。我试着在线搜索,但它们都以dict格式显示输出,即{m-1,a-4,r-1,c-1,l-1,e-2,l-1,b-1,n-2,t-2,o-1,k-2}我才刚刚开始学习编码。所以请帮忙。提前谢谢。

我试过这个

def letter_count(name)
for char in name:
print(f"{char}-{name.count(char)}")

但这对输入bob 不起作用

仍然使用字典,但以更简单的方式,这是O(n^2(

def count_freq(name):
x = dict()
name = name.replace(' ', '')
for i in name:
if i in x.keys():
x[i] += 1
else:
x[i] = 1
b = [f"{k}-{v}" for k, v in x.items()]
print(",".join(b))
name = input("Please insert your input: ")
count_freq(name)

这是一个初学者的O(n(解决方案:

def count_freq(name):
x = dict()
name = name.replace(' ', '')
for i in name:
try:
x[i] += 1
except:
x[i] = 1
b = [f"{k}-{v}" for k, v in x.items()]
print(",".join(b))
name = input("Please insert your input: ")
count_freq(name)

执行时间分析:

#input is 'marcel bentok tanaka'
#string format no-print
theo 9 µs ± 1.37 µs per loop (mean ± std. dev. of 7 runs, 100000 loops each)
**vn2  4.39 µs ± 157 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)**
vn   6.78 µs ± 1.34 µs per loop (mean ± std. dev. of 7 runs, 100000 loops each)
#no string format (counts only)
theo 5.74 µs ± 953 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
**vn2  2.42 µs ± 30 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)**
vn   3.66 µs ± 668 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
#input is a really long text
#string format no-print
theo 20.8 µs ± 1.43 µs per loop (mean ± std. dev. of 7 runs, 100000 loops each)
vn2  106 µs ± 21.9 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
**vn   6.06 µs ± 1.04 µs per loop (mean ± std. dev. of 7 runs, 100000 loops each)**
#no string format (counts only)
theo 16.6 µs ± 576 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
vn2  83 µs ± 21.2 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
**vn   3.22 µs ± 197 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)**

将其设为dict,然后以任意方式将dict转换为字符串。例如,

counts = {}
for char in "abcdefghijklmnopqrstuvwxyz":
counts[char] = myString.count(char)
for key in counts.keys():
print(f"{key}-{counts[char]}")

或者,如果你真的讨厌dicts,那就跳过它:

for char in "abcdefghijklmnopqrstuvwxyz":
print(f"{char}-{myString.count(char)}")

如果你不想显示那些零出现的,请将dict解决方案的最后一部分替换为:

for key in counts.keys():
if counts[char]:
print(f"{key}-{counts[char]}")

这类似于gekigek99的答案


def letter_count(name):
name = string.replace(' ','') # this removes the spaces
name_set = set(new_name) # this creates set of the characters
for char in name_set:
count = str(name.count(char)) # counts how many times $char appeared in $name
print(char + '-' + count + ',', end = '')
#example
name = 'marcel bentok tanaka'
letter_count(name)

你可以试试这个:

def count_freq(name):
name = name.replace(' ','')
output = ''
for char in set(name):
output += (char + '-' + str(name.count(char)) + ',')
return output
name = input('insert name:')
print(count_freq(name))

最新更新