一些Python代码
2020-12-13 16:43
标签:函数 n+1 git eva value pen sorted lse color 统计字符串中的字符个数。未通过 以下为通过代码,注意字典是无序的 判断完全数 统计词频 一些Python代码 标签:函数 n+1 git eva value pen sorted lse color 原文地址:https://www.cnblogs.com/candyYang/p/11621438.htmldef countchar(string):
c_dict = {}
for i in range(26):
c_dict[chr(ord(‘a‘)+i)] = 0
for c in string:
if c in c_dict:
c_dict[c] += 1
return list(c_dict.values())
if __name__ == "__main__":
string = input()
string = string.lower()
print(countchar(string))
def countchar(string):
c_dict = {}
c_list = []
for i in range(26):
c_dict[chr(ord(‘a‘)+i)] = 0
for c in string:
if c in c_dict:
c_dict[c] += 1
c_list = c_dict.items()
c_list= sorted(c_list, key = lambda x:x[0])
c_list = [x[1] for x in c_list]
return c_list
if __name__ == "__main__":
string = input()
string = string.lower()
print(countchar(string))
#判断一个数字是否是全数字
def is_pan(x):
x = str(x)
n = len(x)
flag = True
for i in range(1,n+1):
if str(i) not in x:
flag = False
break
return flag
def pandigital(nums):
lst = []
for x in nums:
if is_pan(x):
lst.append(x)
return lst
if __name__ == "__main__":
lst = pandigital(eval(input()))
#调用函数根据结果输出
for x in lst:
print(x)
if lst == []:
print(‘not found‘)
def countfeq(s):
lst = s.split()
w_dict = {}
for w in lst:
if w not in w_dict:
w_dict[w] = 1
else:
w_dict[w] += 1
return w_dict
if __name__ == "__main__":
s = "Not clumsy person in this world, only lazy people, only people can not hold out until the last."
s = s.replace(‘,‘,‘‘)
s = s.replace(‘.‘,‘‘)
s = s.replace(‘:‘,‘‘)
s = s.replace(‘)‘,‘‘)
s_dict = countfeq(s.lower())
word = input()
#基于s_dict判断word的词频并输出(可能是0次)
if word not in s_dict:
print(0)
else:
print(s_dict[word])
下一篇:python安装及变量设置