统计⼀个字符串中相同字符的个数
作为测试⼩⽩,苦练代码基本功,啥时候都不嫌晚。
今天在测试⾥,看到⼩伙伴在⾯试中有个题⽬,⽤⼀段代码对⼀个字符串进⾏统计,分别输出字符与数量于是⽤业余时间写了下:字符串长度统计
#!/usr/bin/env python
#  -*- coding:utf-8 -*-
message = "abcd12311aacc"
messagelist = []
def CountOfChar(message):
for i in message:
list1 = []
for j in message:
if i == j:
list1.append(i)
if list1 not in messagelist:
messagelist.append(list1)
for i in range(len(messagelist)):
print (messagelist[i][0],len(messagelist[i]))
⽅法⼆:
#!/usr/bin/env python
#  -*- coding:utf-8 -*-
message = "abcd12311aacc"
messagelist = []
def CountOfChar2(message):
for msg in message:
countmess = unt(mes)
msgstr = msg +":"+ str(countmess)
if msgstr not in messagelist:
messagelist.append(msgstr)
for i in messagelist:
print (i)
CountOfChar2(message)

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。