Skip to content

Commit 20b3c59

Browse files
committed
work
1 parent 60ac523 commit 20b3c59

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

exercises/1901100231/1001S02E05_stats_text.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
a[x]=a[x]+1
3939
else:
4040
a[x]=1
41-
for x in sorted(a,key=a.__getitem__,reverse=True):
41+
for x in sorted(a,key=a.__getitem__,reverse=True):#反向排序
4242
print(x,a[x])
4343

4444

exercises/1901100231/1001S02E05_string.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@
2222
Namespaces are one honking great idea -- let's do more of those!
2323
'''#要处理的文本
2424
text = text. replace ("better","worse")#用replace函数进行关键词的替换
25-
text = text. replace ("\n","")#去除换行符,使结果更美观
25+
text = text. replace ("\n"," ")#去除换行符,使结果更美观
26+
text = text. replace ("*","")
27+
text = text. replace ("--","")
28+
text = text. replace (".","")#去除其他符号,否则会影响后续统计
2629
copy = text.split(' ')#将字符串转化为列表,便于之后的操作
27-
for x in copy :#利用循环,查找每一个含有“ea”的单词
28-
if "ea" in x:#利用条件判断,删去每一个符合条件的“x”元素
29-
copy.remove(x)
30+
copy = [x for x in copy if x != '']#去除列表中的空格元素,否则总是有空格会参与排序
31+
copy = [x for x in copy if "ea" not in x]#找出copy中的含“ea”的元素并删去,用循环加条件判断会有漏网之鱼,原因不明
3032
text = ' '.join(copy)#由于大小写转换命令只适用于字符串,故转换回字符串
3133
text=text.swapcase()#进行大小写互换
3234
copy = text.split(' ')#为了排序,在不更改任务顺序的前提下再次转化为列表进行排序

0 commit comments

Comments
 (0)