Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion exercises/1901100166/1001s02e05_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

#3.
b_str=A[2:8]
print('用字符串切片的方式取出第三到八个字符==>',b_str)
print('用字符串切片的方式取出第三到八个字符==>',b_str) #从0 开始算第一个字符

#4.
C=b_str[::-1]
Expand Down
11 changes: 5 additions & 6 deletions exercises/1901100166/1001s02e05_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,25 @@

# 调用 str 类型 的 replace 方法进行替换
b= a.replace('better','worse')
#print('将字符串样本里的 better 全部替换成 worse ==>', b)
print('将字符串样本里的 better 全部替换成 worse ==>', b)

#2.
c=a.split() #调用str类型,将字符串 根据 空白字符 分割成list
guolv=[] #定义list类型的变量存放过滤后的单词
for d in c: #句型判断是否含ea
if d.find('ea')<2: # str类型的find方法 不含参数则返回-1,含则返回第一次出现的索引
if d.find('ea')<0: # str类型的find方法 不含参数则返回-1,含则返回第一次出现的索引
guolv.append(d)
print('剔除ea',guolv)
print('剔除ea==>',guolv)

#3.大小写翻转
E=[e.swapcase() for e in guolv]
#print('333333',E)
print('333333',E)

#4.单词升序排列
#print('升序==>',sorted(E)) #sorted默认升序
print('升序==>',sorted(E)) #sorted默认升序

#print('降序==>',sorted(E, reverse=True))





67 changes: 67 additions & 0 deletions exercises/1901100166/1001s02e06_stats_word.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#统计英文单词出现次数
def stats_text_en(a):
A=a.split()
B=[]
C=',.*-!'

for AA in A:
for CC in C:
AA=AA.replace(CC,'')
if len(AA):
B.append(AA)
counter={}
B_set=set(B)

for BB in B_set:
counter[BB]=B.count(BB)
#函数返回值用return进行返回,如果没有return,则返回值为none
return sorted(counter.items(),key=lambda x: x[1],reverse=True)
#在一个完整的文件里是不能用return值,在函数里可以用?


#统计中文汉字次数

def cn(b):
cn_character=[]
for character in b:
if'\u4e00'<=character<='\u9fff': #汉字在unicode中都有对应固定的值
cn_character.append(character)
counter={}
cn=set(cn_character)
for character in cn:
counter[character]=cn_character.count(character)
return sorted(counter.items(),key=lambda x:x[1],reverse=True)



a= '''
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambxiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
'''



b='我爱北京天安门!'

#if __name__ == "__main__": #这个公式作用:防止在引用这个文件时不运行此代码

print('英文次数==>\n',stats_text_en(a))
print('中文次数==>\n',cn(b))
11 changes: 11 additions & 0 deletions exercises/1901100166/111.py/111.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
a='123456789e'

b=a.split()

print(b)

c=a.replace('1','9')
print(c)

d=a.swapcase()
print(d)