英文字符串

英文字符串操作

 1 import operator
 2 str='hello world'
 3 str1='nihao'
 4 #去空格以及特殊字符
 5 print(str.strip())
 6 
 7 #链接字符串
 8 print(str+str1)
 9 
10 #查找字符
11 print(str.index('w'))
12 
13 #比较
14 print(operator.eq(str,str1))
15 
16 #大小写转换
17 print(str.upper())
18 
19 #翻转字符串
20 print(str[::-1])
21 
22 #查找字符串
23 print(str.find(str1))
24 
25 #分割字符串
26 print(str.split(' '))
27 
28 #计算字符串中出现频次最多的字母
29 import re
30 from collections import Counter
31 
32 #第一种方法
33 def get_max_value_v1(text):
34     text=text.lower()
35     result=re.findall('[a-zA-Z]',text) #去掉列表中的符号符
36     count=Counter(result)
37     count_list=list(result)
38     max_value=max(count_list)
39     max_list=[]
40     for k,v in count.items():
41         if v==max_value:
42             max_list.append(k)
43 
44     max_list=sorted(max_list)
45     return max_list[0]
46 
47 #第二种方法
48 def get_max_value(text):
49     count=Counter([x for x in text.lower() if x.isalpha()])
50     m=max(count.values())
51     return sorted([x for(x,y) in count.items() if y==m])[0]
52 
53 #第三种方法
54 import string
55 def get_max_value3(text):
56     text=text.lower()
57     return max(string.ascii_lowercase,key=text.count)
58 
59 print(get_max_value3('fejfoefdfefidof32poe'))
View Code

 

posted @ 2018-03-05 09:22  88aa123  阅读(182)  评论(0编辑  收藏  举报