python(2)带解析和要点

以下题目均需通过字符串方法进行操作,直接人工计算不得分!
In [ ]:

xxxxxxxxxx

#运行一~四题前请先运行此段代码
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.”
. . .

xxxxxxxxxx

一、统计en_text中to的出现次数
一、统计en_text中to的出现次数
In [ ]:

xxxxxxxxxx

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)

. . .

xxxxxxxxxx

二、将en_text分割成几个句子(以句号为准),依次打印。
二、将en_text分割成几个句子(以句号为准),依次打印。
In [ ]:

xxxxxxxxxx

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)

. . .

xxxxxxxxxx

三、将en_text分割出所有的单词(不能包含逗号、句号),统计每个单词出现的次数
三、将en_text分割出所有的单词(不能包含逗号、句号),统计每个单词出现的次数
In [ ]:

xxxxxxxxxx

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)

. . .

xxxxxxxxxx

四、接上题,统计所有单词的首字母的出现次数
四、接上题,统计所有单词的首字母的出现次数
In [ ]:

xxxxxxxxxx

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)

. . .

xxxxxxxxxx

五、输入身份证号,输出此人的出生年月日。注意限制输入的身份证号只能为18位,第7位开始即为出生年月日
五、输入身份证号,输出此人的出生年月日。注意限制输入的身份证号只能为18位,第7位开始即为出生年月日
In [ ]:

xxxxxxxxxx

n = input(“请输入身份证号”)
a=n[6:10]#前面包括后面不包括
b=n[10:12]
c=n[12:14]
print(a,b,c)

. . .

xxxxxxxxxx

六、将一个字符串的所有数字去除,产生一个新的字符串
六、将一个字符串的所有数字去除,产生一个新的字符串
In [ ]:

xxxxxxxxxx

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’]其实道理就是把遍历一个个的捆绑在一起

. . .

xxxxxxxxxx

七、运用字符串的各种操作来处理scores,得出平均成绩
七、运用字符串的各种操作来处理scores,得出平均成绩
In [ ]:

xxxxxxxxxx

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)

. . .

xxxxxxxxxx

八、根据一个收件人列表,输出

姓名:xxx 电话:xxx 收货地址:xxx

姓名:xxx 电话:xxx 收货地址:xxx

的格式

八、根据一个收件人列表,输出
姓名:xxx 电话:xxx 收货地址:xxx
姓名:xxx 电话:xxx 收货地址:xxx
的格式
In [ ]:

xxxxxxxxxx

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]))

. . .

xxxxxxxxxx

以下题目为函数专项练习,不能出现任何for、while循环语句,用map、reduce、filter、sorted等代替

以下题目为函数专项练习,不能出现任何for、while循环语句,用map、reduce、filter、sorted等代替¶

xxxxxxxxxx

九、用map实现将一个字符串里所有的单词首字母变成大写,最后输出一个单词列表。

提示:

将字母转成大写:b = a.upper() a是要转的字母,b是转化结果
九、用map实现将一个字符串里所有的单词首字母变成大写,最后输出一个单词列表。
提示:
将字母转成大写:b = a.upper() a是要转的字母,b是转化结果
In [ ]:

xxxxxxxxxx

在这里插入代码片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)

. . .

xxxxxxxxxx

十、用filter、map、sorted实现将一个列表里不是纯数字的内容去掉,再全部转成数字类型,最后从小到大排序
十、用filter、map、sorted实现将一个列表里不是纯数字的内容去掉,再全部转成数字类型,最后从小到大排序
In [ ]:

xxxxxxxxxx

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)
. . .

xxxxxxxxxx

十一、用map得到工资表里每个人的收入(工资+奖金),并用reduce得出所有人的总收入
十一、用map得到工资表里每个人的收入(工资+奖金),并用reduce得出所有人的总收入
In [2]:

xxxxxxxxxx

salary = [
“姓名:张三 部门:销售 工资:3000 奖金:8000”,
“姓名:李四 部门:研发 工资:7000 奖金:2000”,
“姓名:王五 部门:运营 工资:5000 奖金:4000”,
]

def xinzi(zong):
date = zong.split()
b = date[0].split(’:’)[1]
a = int(date[2].split(’:’)[1])+int(date[3].split(’:’)[1])
return b,‘工资’,a
date2 = list(map(xinzi,salary))
print(date2)

[(‘张三’, ‘工资’, 11000), (‘李四’, ‘工资’, 9000), (‘王五’, ‘工资’, 9000)]
. . .

xxxxxxxxxx

十二、继续使用salary表,使用filter筛选出收入超过1万的员工,打印他的姓名和收入
十二、继续使用salary表,使用filter筛选出收入超过1万的员工,打印他的姓名和收入
In [3]:

xxxxxxxxxx

salary = [
“姓名:张三 部门:销售 工资:3000 奖金:8000”,
“姓名:李四 部门:研发 工资:7000 奖金:2000”,
“姓名:王五 部门:运营 工资:5000 奖金:4000”,
]
#[(11000, ‘张三’), (9000, ‘李四’), (9000, ‘王五’)]
def xinzi(zong):
date = zong.split()
b = date[0].split(’:’)[1]
a = int(date[2].split(’:’)[1])+int(date[3].split(’:’)[1])
if a >=10000:
c =a
return b,a

date2 = list(map(xinzi,salary))
date3 = list(filter(xinzi,salary))

print(date2)

[(‘张三’, 11000), None, None]
. . .

xxxxxxxxxx

十三、继续使用salary表,年会给每个员工随机抽奖,有不中奖、奖100、奖500、奖1000四种情况,概率相等。输出年会过后每个人的收入
十三、继续使用salary表,年会给每个员工随机抽奖,有不中奖、奖100、奖500、奖1000四种情况,概率相等。输出年会过后每个人的收入
In [ ]:

xxxxxxxxxx

import random
salary = [
“姓名:张三 部门:销售 工资:3000 奖金:8000”,
“姓名:李四 部门:研发 工资:7000 奖金:2000”,
“姓名:王五 部门:运营 工资:5000 奖金:4000”,
]
def ka(la):
date = la.split()
a = date[0].split(’:’)[1]
b = int(date[2].split(’:’)[1])+int(date[3].split(’:’)[1])
c = random.randint(1,4)
if c == 1:
b=b+1000
return a,b
elif c ==2:
b=b+100
return a,b
elif c == 3:
b=b+500
return a,b
else:
return a,b
date2 = list(map(ka,salary))
print(date2)

. . .

xxxxxxxxxx

十四、继续使用salary表,使用带条件的sorted实现对salary按工资从小到大排序,输出如下新列表:

salary1 = [
“姓名:张三 部门:销售 工资:3000 奖金:8000”,
“姓名:王五 部门:运营 工资:5000 奖金:4000”,
“姓名:李四 部门:研发 工资:7000 奖金:2000”,
]
十四、继续使用salary表,使用带条件的sorted实现对salary按工资从小到大排序,输出如下新列表:
salary1 = [ “姓名:张三 部门:销售 工资:3000 奖金:8000”, “姓名:王五 部门:运营 工资:5000 奖金:4000”, “姓名:李四 部门:研发 工资:7000 奖金:2000”, ]
In [ ]:

xxxxxxxxxx

salary = [
“姓名:张三 部门:销售 工资:3000 奖金:8000”,
“姓名:李四 部门:研发 工资:7000 奖金:2000”,
“姓名:王五 部门:运营 工资:5000 奖金:4000”,
]
def ka(la):
date = la.split()
b =int(date[2].split(’:’)[1])
return b
date2 = (sorted(salary,key=ka))
print(date2)

. . .

xxxxxxxxxx

十五、处理以下成绩表,用带条件的sorted方法将数据按年级从小到大排序
十五、处理以下成绩表,用带条件的sorted方法将数据按年级从小到大排序
In [ ]:

xxxxxxxxxx

scores = [
“姓名:小红 年级:2 语文:79 数学:88 英语:72”,
“姓名:小黄 年级:1 语文:84 数学:82 英语:90”,
“姓名:小黑 年级:1 语文:82 数学:78 英语:92”,
“姓名:小蓝 年级:3 语文:75 数学:90 英语:81”,
“姓名:小绿 年级:2 语文:81 数学:86 英语:83”,
“姓名:小金 年级:2 语文:90 数学:75 英语:80”,
]
def akk(bab):
dao = bab.split()
a = dao[1].split("😊[1]
return a
l1 = list(sorted(scores,key=akk))
print(l1)

. . .

xxxxxxxxxx

十六、继续使用scores表,使用map得出所有人的总分,输出姓名:xxx 总分:xxx 的格式
十六、继续使用scores表,使用map得出所有人的总分,输出姓名:xxx 总分:xxx 的格式
In [ ]:

xxxxxxxxxx

scores = [
“姓名:小红 年级:2 语文:79 数学:88 英语:72”,
“姓名:小黄 年级:1 语文:84 数学:82 英语:90”,
“姓名:小黑 年级:1 语文:82 数学:78 英语:92”,
“姓名:小蓝 年级:3 语文:75 数学:90 英语:81”,
“姓名:小绿 年级:2 语文:81 数学:86 英语:83”,
“姓名:小金 年级:2 语文:90 数学:75 英语:80”,
]
def akk(bab):
dao = bab.split()
a = dao[0].split("😊[1]
b =int(dao[2].split("😊[1])+int(dao[3].split("😊[1])+int(dao[4].split("😊[1])
return a,b
l1 = list(map(akk,scores))
print(l1)

. . .

xxxxxxxxxx

十七、继续使用scores表,使用map得出所有人的数学分数,再用reduce得出数学最高的分数
十七、继续使用scores表,使用map得出所有人的数学分数,再用reduce得出数学最高的分数
In [4]:

xxxxxxxxxx

from functools import reduce
scores = [
“姓名:小红 年级:2 语文:79 数学:88 英语:72”,
“姓名:小黄 年级:1 语文:84 数学:82 英语:90”,
“姓名:小黑 年级:1 语文:82 数学:78 英语:92”,
“姓名:小蓝 年级:3 语文:75 数学:90 英语:81”,
“姓名:小绿 年级:2 语文:81 数学:86 英语:83”,
“姓名:小金 年级:2 语文:90 数学:75 英语:80”,
]
def akk(bab):
dao = bab.split()
a = dao[0].split("😊[1]
b =int(dao[3].split("😊[1])
return a,b
l1 = list(map(akk,scores))
print(l1)
def akk(bab):
dao = bab.split()
b =int(dao[3].split("😊[1])
return b
l2 = list(map(akk,scores))
l2 = list(map(int,l2))
def abd(x,y):

return max(x,y)

print(reduce(abd,l2))

[(‘小红’, 88), (‘小黄’, 82), (‘小黑’, 78), (‘小蓝’, 90), (‘小绿’, 86), (‘小金’, 75)]
90
. . .

xxxxxxxxxx

十八、继续使用scores表,使用filter筛选出所有2年级的学生,再用带条件的sorted对其按语文分数从低到高排序
十八、继续使用scores表,使用filter筛选出所有2年级的学生,再用带条件的sorted对其按语文分数从低到高排序
In [ ]:

xxxxxxxxxx

scores = [
“姓名:小红 年级:2 语文:79 数学:88 英语:72”,
“姓名:小黄 年级:1 语文:84 数学:82 英语:90”,
“姓名:小黑 年级:1 语文:82 数学:78 英语:92”,
“姓名:小蓝 年级:3 语文:75 数学:90 英语:81”,
“姓名:小绿 年级:2 语文:81 数学:86 英语:83”,
“姓名:小金 年级:2 语文:90 数学:75 英语:80”,
]
def akk(bab):#先一起输出二年级
dao = bab.split()
a = dao[0].split("😊[1]
b =int(dao[1].split("😊[1])
if b == 2:
return a
l1 = list(filter(akk,scores))
def www(bab):#只留下名字
dao = bab.split()
a = dao[0].split("😊[1]
return a
l5 =list(map(www,l1))
print(“二年级学生”,l5)
def dsfds(bab):#先一起排列
dao = bab.split()
a = dao[0].split("😊[1]
b =int(dao[2].split("😊[1])
return a,b
l4 = sorted(l1,key=dsfds)
def psda(bab):#只留下名字加成绩
dao = bab.split()
a = dao[0].split("😊[1]
b =int(dao[2].split("😊[1])
return a,b
l2 = list(map(psda,l4))
print(l2)

. . .

xxxxxxxxxx

十九、使用filter和reduce计算1~100中所有奇数的平方和
十九、使用filter和reduce计算1~100中所有奇数的平方和
In [ ]:

xxxxxxxxxx

from functools import reduce
nums = list(range(1,101))
def jj(x):
a=0
if x%2==0:
return 0
else:
a += x**3
return a
l1 =list(map(jj,nums))
def sl(x,y):
return x+y
l2 = reduce(sl,l1)
print(l2)

. . .

xxxxxxxxxx

二十、使用map和filter筛选出100~999中所有的水仙花数(各个位上的数立方和等于数字本身)
二十、使用map和filter筛选出100~999中所有的水仙花数(各个位上的数立方和等于数字本身)
In [ ]:

xxxxxxxxxx

#生成所有的三位数列表
nums = list(range(100,1000))
def kk(x):
a = x%10
b = x//10%10
c = x//100
if a3+b3+c**3==x:
return int(x)
b = list(filter(kk,nums))
print(b)
. . .
Def 函数中的结构
return max(x,y)
return “”.join(filter(str.isdigit,j))
h+=eval(i)#求和
salary = [
“姓名:张三 部门:销售 工资:3000 奖金:8000”,
“姓名:李四 部门:研发 工资:7000 奖金:2000”,
“姓名:王五 部门:运营 工资:5000 奖金:4000”,
]

b = date[0].split(’:’)[1]
a = int(date[2].split(’:’)[1])+int(date[3].split(’:’)[1])
s = “hello world hello python”
return daxie.capitalize()
return
kao1[0].upper()+kao[1:].lower()

容器操作for 的操作
en_text = en_text.replace(’,’,’ ‘).replace(’.’,’ ')
text = en_text.split()
i in d

d = {}
for i in text:
if i in d:
d[i]+=1
else:
d[i]=1
print(d)

for j in i[:1]:

g = [ ] for d in i:
g.append(d)

import random
c = random.randint(1,4)

if I.isdigit/alpha() #判断 I 是否为数字或者字母

map reduce sorted filter 的调用

from functools import reduce

l1=reduce(函数,容器)

l2=list(map(函数,容器))函数可以=int输出类型

date2 = (sorted(容器,key=函数))
l3.sort()

l1=list(filter(函数,容器))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

南师大蒜阿熏呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值