1.统计en_text中to的出现次数
en_text = "The first step is one of awareness. It will be hard to make a change to positive thinking without being acutely intimate with the thoughts that run through your mind. Recently, I was amazed to discover deep buried emotions from negative thoughts that I had for fewer than 10 minutes. Without awareness, I would have carried the hurt and anger inside. Awareness helped me to bring them out to the open for me to deal with."
en_text = en_text.replace(',',' ').replace('.',' ')
text = en_text.split()
d = "to"
c=0
for i in text:
if i in d:
c+=1
print(d,c)
2。将en_text分割成几个句子(以句号为准),依次打印。
en_text = "The first step is one of awareness. It will be hard to make a change to positive thinking without being acutely intimate with the thoughts that run through your mind. Recently, I was amazed to discover deep buried emotions from negative thoughts that I had for fewer than 10 minutes. Without awareness, I would have carried the hurt and anger inside. Awareness helped me to bring them out to the open for me to deal with."
text = en_text.split('.')
for i in text:
print(i)
3.将en_text分割出所有的单词(不能包含逗号、句号),统计每个单词出现的次数
en_text = "The first step is one of awareness. It will be hard to make a change to positive thinking without being acutely intimate with the thoughts that run through your mind. Recently, I was amazed to discover deep buried emotions from negative thoughts that I had for fewer than 10 minutes. Without awareness, I would have carried the hurt and anger inside. Awareness helped me to bring them out to the open for me to deal with."
en_text = en_text.replace(',',' ').replace('.',' ')
text = en_text.split()
d = {}
for i in text:
if i in d:
d[i]+=1
else:
d[i]=1
print(d)
4.接上题,统计所有单词的首字母的出现次数
en_text = "The first step is one of awareness. It will be hard to make a change to positive thinking without being acutely intimate with the thoughts that run through your mind. Recently, I was amazed to discover deep buried emotions from negative thoughts that I had for fewer than 10 minutes. Without awareness, I would have carried the hurt and anger inside. Awareness helped me to bring them out to the open for me to deal with."
en_text = en_text.replace(',',' ').replace('.',' ')
text = en_text.split()
d = {}
for i in text:
for j in i[:1]:
if j in d:
d[j]+=1
else:
d[j]=1
print(d)
5.、输入身份证号,输出此人的出生年月日。注意限制输入的身份证号只能为18位,第7位开始即为出生年月日
n = input("请输入身份证号")
a=n[6:10]#前面包括后面不包括
b=n[10:12]
c=n[12:14]
print(a,b,c)
6.、将一个字符串的所有数字去除,产生一个新的字符串
s = "asdf1234jkl56qwe789xyz"
g = []
for i in s:
if i.isalpha():
for d in i:
g.append(d)
print("".join(g))#结尾这个我想了好久,如果直接print(g)输出的那个结果是这样的['a', 's', 'd', 'f', 'j', 'k', 'l', 'q', 'w', 'e', 'x', 'y', 'z']其实道理就是把遍历一个个的捆绑在一起
7.运用字符串的各种操作来处理scores,得出平均成绩
scores = "小红-86,小黄-90,小蓝-78,小绿-80,小黑-84"
scores = scores.replace(',',' ').replace('-',' ')
a=scores.split()
b=0
c=[]
h=0
k=0
for i in a:
if i.isdigit():
b+=1
c.append(i)
for i in c:
h+=eval(i)
k=h/b
print(k)
八、根据一个收件人列表,输出 姓名:xxx 电话:xxx 收货地址:xxx 姓名:xxx 电话:xxx 收货
infos = [
"张三 北京市海淀区学霸村一巷666号 13466666666",
"李四 上海市浦东新区土豪路888号 13988888888",
"王五 深圳市龙岗区加班园C栋 13899677996",
]
for i in infos:
str_cpoy = i.split(' ')
print("name:{} phone:{} address:{}".format(str_cpoy[0],str_cpoy[2],str_cpoy[1]))
9.用map实现将一个字符串里所有的单词首字母变成大写,最后输出一个单词列表。 提示: 将字母转成大写:b = a.upper() a是要转的字母,b是转化结果
在这里插入代码片s = "hello world hello python"
def aple(daxie):
return daxie.capitalize()
l1=s.split()
l2=list(map(aple,l1))
print(l2)
#或者
def papa(kao):
return
kao1[0].upper()+kao[1:].lower()
l1=s.split()
l2=list(map(aple,l1))
print(l2)
10.用filter、map、sorted实现将一个列表里不是纯数字的内容去掉,再全部转成数字类型,最后从小到大排序
data = ["34","a12","78j","10","8","50","18","abc","24"]
def app(j):#这个函数是把“abc”去掉
return "".join(filter(str.isdigit,j))
l1=list(filter(app,data))
def kak(k):#这个函数是把“a12”里面的字母去掉
return "".join(filter(str.isdigit,k))
l2 = list(map(kak,l1))
print(l2)
l3=list(map(int,l2))#这一步在排序里至关重要,妈的想到吐血都想不明白为毛8最大,因为没有确定类型,这里确定了一个整的,计算机读不懂“43”是数字还是毛线所以要变一下
l3.sort()
print(l3)