【python】字符串基础练习题(3)

1. 定义一个变量News,内容为如下【】内的内容。

【The UN Security Council (UNSC) on Sunday adopted Resolution 2623 that calls for an “emergency special session” of the UN General Assembly on the Ukraine crisis. It is the first such a resolution that the council has adopted in four decades, according to the Security Council Report. The vote by the 15 member council was procedural, and the resolution convening the General Assembly session was adopted with 11 “yes” votes. Russia voted “no” while the United Arab Emirates, India and China abstained.】
请输出News中每个字符出现的次数。

News = r'''The UN Security Council (UNSC) on Sunday adopted Resolution 2623 that calls for an "emergency special session" of the UN General Assembly on the Ukraine crisis. It is the first such a resolution that the council has adopted in four decades, according to the Security Council Report. The vote by the 15 member council was procedural, and the resolution convening the General Assembly session was adopted with 11 "yes" votes. Russia voted "no" while the United Arab Emirates, India and China abstained.'''

dict = {}   # 定义一个字典,用来存放统计结果
for i in News:
    if i in dict:
        dict[i] += 1    # 当i已在dict内,加上1
    else:
        dict[i] = 1     #当i不在dict内,赋值1

# 换行输出
for i in dict:
    print('{}:{}次'.format(i,dict[i]))

以下题目如非必要,将不再重复显示News的内容。且每一题都与上一题有一定关联,变量的引用为上一题的答案。

2. 将News中的每个单词分隔出来(包括标点符号,以空格为界)到News_split中并输出。
News_split = News.split()   # split():将一个字符串分隔成多个字符串组成的列表
print(News_split)
3. 记录News所有的标点符号到列表punc中。
punc = []
for i in News:
   # 判断是否为字母、数字或空格
    if i.isalnum() == False and i.isspace() == False:
        punc += i
print(punc)
4. 根据punc,将News_split中所有元素两头的标点符号去掉,并输出。
str = ""
for i in ' '.join(News_split):
    if i not in ''.join(punc):
        str += i	# 根据punc,如果是标点符号,则不加入到str中
News_split = str.split()
print(News_split)
5. 根据以下规则把News_split中所有元素中所有数字替换为大写英文字母:“1234567890”<=>“ABCDEFGHIJ”,并输出。
table = str.maketrans('1234567890','ABCDEFGHIJ')	# 使用maketrans创建字符映射转换表,本质上是生成一个字典table,字典的key和value均以ASCII码表示。
# maketrans的具体用法可见https://www.cnblogs.com/wushuaishuai/p/7687074.html
News_split = ' '.join(News_split).translate(table).split()	# 将News_split先转成字符串进行处理,生成的新字符串再转成列表
print(News_split)
6. 把News_split每个元素中的大小写进行替换,大写变成小写,小写变成大写,并输出。
# 将News_split转成字符串,然后swapcase()大小写转换,最后转回列成输出
News_split = ((' '.join(News_split)).swapcase()).split()
print(News_split)
7. 将替换后的News_split的元素中的所有小写字母形成新的字符串News_lower,并输出。
News_lower = ""
for i in News_split:
    for j in i:
        if j.islower():     # 当字母为小写,即为True,则进行连接
            News_lower += ''.join(j)
print(News_lower)
8. 将News_lower以u为分隔符,分成n个字符串,存到News_lower_u中,找到元素中长度最长的字符串,将其他字符串在左边补字母’q’补齐到跟该字符串一样长,输出News_lower_u。
News_lower_u = News_lower.split('u')
a = ""
list = []
len_max = len(max(News_lower_u,key=len))	# 取出News_lower_u里最长的字符串,其长度赋给len_max
for str in News_lower_u:
    a += str.rjust(len_max,'q')		# 字符串右对齐,左边补齐q
    if len(a) % len_max == 0:		# 此时a为string类型,此处if的功能是将a按照规定的长度进行切割
        list.append(a)		# 切割得到的字符串存进list类型的list中
        a = ""				# 对a做一个初始化清空,以便循环存入新数据
News_lower_u = list
print(News_lower_u)
9. 根据以下替换表规则对News_lower_u进行替换:

‘abcdefghijklmnopqrstuvwxyz’
‘cbadeirhfjksvwxyztlgumnopq’
并去掉其中所有原字符串中的’a’、‘q’、'r’都去掉

table_1 = str.maketrans('abcdefghijklmnopqrstuvwxyz','cbadeirhfjksvwxyztlgumnopq','aqr')
News_lower_u = ' '.join(News_lower_u).translate(table_1).split()
print(News_lower_u)
10. 找到News_lower_u中以’flag’开头的那个元素,将其与News_lower_u中最长的元素拼接,并以News_lower_u中第二短的元素拼接到结尾,最后对该元素进行以下规则的替换:

{‘a’:‘a’, ‘b’:‘_’, ‘e’:‘{’, ‘f’:‘F’, ‘g’:‘g’, ‘i’:‘Easy’, ‘l’:‘l’, ‘r’:‘noon}’, ‘w’:‘fter’}
输出flag。

for i in News_lower_u:
    if 'flag' in i:
        str_1 = i
str_2 = max(News_lower_u,key=len)
News_lower_u_add = sorted(News_lower_u, key=lambda x:len(x))
str_3 = News_lower_u_add[1]     # str_3:第二短的元素
str_add = str_1 + str_2 + str_3     # str_add:拼接起来的字符串
table_2 = {'a':'a', 'b':'_', 'e':'{', 'f':'F', 'g':'g', 'i':'Easy', 'l':'l', 'r':'noon}', 'w':'fter'}
table_2 = str.maketrans(table_2)
print(str_add.translate(table_2))


以下为以上十题的代码整合:

News = r'''The UN Security Council (UNSC) on Sunday adopted Resolution 2623 that calls for an "emergency special session" of the UN General Assembly on the Ukraine crisis. It is the first such a resolution that the council has adopted in four decades, according to the Security Council Report. The vote by the 15 member council was procedural, and the resolution convening the General Assembly session was adopted with 11 "yes" votes. Russia voted "no" while the United Arab Emirates, India and China abstained.'''

dict = {}   # 定义一个字典,用来存放统计结果
for i in News:
    if i in dict:
        dict[i] += 1    # 当i已在dict内,加上1
    else:
        dict[i] = 1     #当i不在dict内,赋值1

# 换行输出
for i in dict:
    print('{}:{}次'.format(i,dict[i]))

News_split = News.split()   # split():将一个字符串分隔成多个字符串组成的列表
print(News_split)

punc = []
for i in News:
   # 判断是否为字母、数字或空格
    if i.isalnum() == False and i.isspace() == False:
        punc += i
print(punc)

str = ""
for i in ' '.join(News_split):
    if i not in ''.join(punc):
        str += i	# 根据punc,如果是标点符号,则不加入到str中
News_split = str.split()
print(News_split)

table = str.maketrans('1234567890','ABCDEFGHIJ')	# 使用maketrans创建字符映射转换表,本质上是生成一个字典table,字典的key和value均以ASCII码表示。
# maketrans的具体用法可见https://www.cnblogs.com/wushuaishuai/p/7687074.html
News_split = ' '.join(News_split).translate(table).split()	# 将News_split先转成字符串进行处理,生成的新字符串再转成列表
print(News_split)

# 将News_split转成字符串,然后swapcase()大小写转换,最后转回列成输出
News_split = ((' '.join(News_split)).swapcase()).split()
print(News_split)

News_lower = ""
for i in News_split:
    for j in i:
        if j.islower():     # 当字母为小写,即为True,则进行连接
            News_lower += ''.join(j)
print(News_lower)

News_lower_u = News_lower.split('u')
a = ""
list = []
len_max = len(max(News_lower_u,key=len))	# 取出News_lower_u里最长的字符串,其长度赋给len_max
for str in News_lower_u:
    a += str.rjust(len_max,'q')		# 字符串右对齐,左边补齐q
    if len(a) % len_max == 0:		# 此时a为string类型,此处if的功能是将a按照规定的长度进行切割
        list.append(a)		# 切割得到的字符串存进list类型的list中
        a = ""				# 对a做一个初始化清空,以便循环存入新数据
News_lower_u = list
print(News_lower_u)

table_1 = str.maketrans('abcdefghijklmnopqrstuvwxyz','cbadeirhfjksvwxyztlgumnopq','aqr')
News_lower_u = ' '.join(News_lower_u).translate(table_1).split()
print(News_lower_u)

for i in News_lower_u:
    if 'flag' in i:
        str_1 = i
str_2 = max(News_lower_u,key=len)
News_lower_u_add = sorted(News_lower_u, key=lambda x:len(x))
str_3 = News_lower_u_add[1]     # str_3:第二短的元素
str_add = str_1 + str_2 + str_3     # str_add:拼接起来的字符串
table_2 = {'a':'a', 'b':'_', 'e':'{', 'f':'F', 'g':'g', 'i':'Easy', 'l':'l', 'r':'noon}', 'w':'fter'}
table_2 = str.maketrans(table_2)
print(str_add.translate(table_2))

本篇练习的答案请见:https://blog.csdn.net/CaiDeWei/article/details/124021742

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

CHOITAKWAI

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

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

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

打赏作者

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

抵扣说明:

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

余额充值