Emma Woodhouse, handsome, clever, and rich, with a comfortable home and happy disposition, seemed to unite some of the best blessings of existence; and had lived nearly twenty-one years in the world with very little to distress or vex her.
针对上述语料,请完成以下操作:
(1) 统计上述文本中共有多少个单词:
a = "Emma Woodhouse, handsome, clever, and rich, with a comfortable home and happy disposition, seemed to unite some of the best blessings of existence; and had lived nearly twenty-one years in the world with very little to distress or vex her."
b = a.replace(',','')
c = b.replace('.','') #替掉(或者说是删除掉)逗号和句号,为下一步进入列表的规范化做准备(其实是为了第二问做准备)
d = c.split(' ') #空格分隔单词输入列表
print(len(d)) #输出列表元素数量
(2) 输出上述文本中所有长度为4个字母的单词:
list_i = [] #先预设一个空列表
for i in d:
if len(i) == 4:
list_i.append(i) #经典的列表遍历,查找长度并将查找结果输入新列表
print(list_i)
(3) 将上一步输出的单词分别以首字母大写、全大写、全小写的方式输出:
for word in list_i:
print(word.title(),word.upper(),word.lower()) #毋庸多言
这样上传一下云端,顺便分享一波,还望指教。