在以下文本中找出 每行中长度超过3的单词
a=’’‘Call me Ishmael. Some years ago - never mind how long precisely - having
little or no money in my purse, and nothing particular to interest me
on shore, I thought I would sail about a little and see the watery part
of the world. It is a way I have of driving off the spleen, and regulating
the circulation. - Moby Dick’’’
首选需要明白的一个split函数的语法及作用
语法:
str.split(str="", num=string.count(str)).
str – 分隔符,默认为所有的空字符,包括空格、换行(\n)、制表符(\t)等。
num – 分割次数。默认为 -1, 即分隔所有。
其实说白了,就是拿什么分割你的字符串,然后又要分割几次
a='''Call me Ishmael. Some years ago - never mind how long precisely - having
little or no money in my purse, and nothing particular to interest me
on shore, I thought I would sail about a little and see the watery part
of the world. It is a way I have of driving off the spleen, and regulating
the circulation. - Moby Dick'''
list1=[]
for i in a.split('\n'):
print(i)
执行结果如下,按换行分割了
a='''Call me Ishmael. Some years ago - never mind how long precisely - having
little or no money in my purse, and nothing particular to interest me
on shore, I thought I would sail about a little and see the watery part
of the world. It is a way I have of driving off the spleen, and regulating
the circulation. - Moby Dick'''
list1=[]
for i in a.split('\n'):
for j in i.split(' '):
if len(j)>3:
list1.append(j)
print(list1)
输出结果为满足超过3个字母的所有单词
之前有说过列表推导式,其实就可以很简单的完成整个
a='''Call me Ishmael. Some years ago - never mind how long precisely - having
little or no money in my purse, and nothing particular to interest me
on shore, I thought I would sail about a little and see the watery part
of the world. It is a way I have of driving off the spleen, and regulating
the circulation. - Moby Dick'''
list1=[[j for j in i.split(' ') if len(j)>3 ]for i in a.split('\n')]
print(list1)
结果也是一样的