欧德奈瑞 发表于 2020-5-21 09:09:57

字符串的美化 (题目类型:Python字符串与正则表达式)

本帖最后由 欧德奈瑞 于 2020-5-21 19:19 编辑

7-1 字符串的美化
输入一行以空格作为分隔符的任意字符串,对其按下列要求处理后输出:

1、删除所有的数字字符
2、删除单词中的破折号-
3、删除以@和#字符开头的单词
4、删除单词尾部有三个点的省略号(...)
输入格式:
输入非空的字符串。

输出格式:
输出处理后的字符串

输入样例:
在这里给出一组输入。例如:

#1: welcome @tom to the2 Basic-building...
输出样例:
在这里给出相应的输出。例如:

welcome to the Basicbuilding

Twilight6 发表于 2020-5-21 09:12:07

骚等~

欧德奈瑞 发表于 2020-5-21 09:14:51

Twilight6 发表于 2020-5-21 09:12
骚等~

静候大佬

Twilight6 发表于 2020-5-21 09:24:37

欧德奈瑞 发表于 2020-5-21 09:14
静候大佬

old_str = input('请输入需要美化的字符串:')
new_str = ''
for i in old_str:
    if i.isdigit() or i == '-': # 判断是否是数字 或 破折号
      continue
    new_str += i
cut_str = new_str.split(' ')

for i in cut_str:
    if i == '@' or i == '#':
      cut_str.remove(i)
    ifi[-3:] == '...':
      temp = i[:-3]
      cut_str.insert(cut_str.index(i),temp)
      cut_str.remove(i)


new_str = ' '.join(cut_str)
print(new_str)

sunrise085 发表于 2020-5-21 09:35:22

本帖最后由 sunrise085 于 2020-5-21 09:41 编辑

def clear_word(str1):
    list1=str1.split()   # 先按照空格拆分成单词
    for i in range(len(list1)-1,-1,-1):# 遍历所有单词,逆序遍历,因为可能会删除某个单词从而改变后面单词的下标值
      if list1=='@' or list1=='#':# 若单词以@或者#开头,则删除该单词
            list1.pop(i)
      else:      # 否则按照规则处理单词,下面这三种不分先后,可以换顺序
            for j in range(10):   # 先清除单词中的数字
                list1="".join(list1.split(str(j)))
            list1="".join(list1.split('-'))# 再清除单词中的破折号
            list1=list1.rstrip('...')   # 最后再清除结尾的省略号
    return ' '.join(list1) # 把单词还原成字符串并返回
   
str1=input()
print(clear_word(str1))

欧德奈瑞 发表于 2020-5-21 09:37:10

Twilight6 发表于 2020-5-21 09:24


大佬,你的代码感觉特别棒,完全能过已给的样例。就是我的测试点没有全过,隐藏起来的另外两个样例(我也不知道是什么)显示“非零返回”。难道是没用正则表达式?

Twilight6 发表于 2020-5-21 09:38:37

欧德奈瑞 发表于 2020-5-21 09:37
大佬,你的代码感觉特别棒,完全能过已给的样例。就是我的测试点没有全过,隐藏起来的另外两个样例(我也 ...

嗷嗷哦 等等我在改改 我美发现是使用正则

欧德奈瑞 发表于 2020-5-21 09:39:06

sunrise085 发表于 2020-5-21 09:35


大佬,结果错误呀。显示
['#1:', 'welcome', '@tom', 'to', 'the2', 'Basic-building...']
#1: welcome to the Basicbuilding

欧德奈瑞 发表于 2020-5-21 09:39:51

Twilight6 发表于 2020-5-21 09:38
嗷嗷哦 等等我在改改 我美发现是使用正则

静候佳音

sunrise085 发表于 2020-5-21 09:42:30

欧德奈瑞 发表于 2020-5-21 09:39
大佬,结果错误呀。显示
['#1:', 'welcome', '@tom', 'to', 'the2', 'Basic-building...']
#1: welcome ...

我刚刚加了一下注释,你再试一下,我这里没问题啊。
难道之前复制粘贴的时候有错误的地方?

欧德奈瑞 发表于 2020-5-21 09:44:12

sunrise085 发表于 2020-5-21 09:42
我刚刚加了一下注释,你再试一下,我这里没问题啊。
难道之前复制粘贴的时候有错误的地方?

嗯,我这也能过我给的样本1。但是就是不知道题目被隐藏了什么另外的两个样本,测试第二个样本的时候显示“格式错误”。

sunrise085 发表于 2020-5-21 09:45:44

欧德奈瑞 发表于 2020-5-21 09:44
嗯,我这也能过我给的样本1。但是就是不知道题目被隐藏了什么另外的两个样本,测试第二个样本的时候显示 ...

其他样本是啥?

欧德奈瑞 发表于 2020-5-21 09:47:16

sunrise085 发表于 2020-5-21 09:45
其他样本是啥?

额, 大佬,我这也没显示其他样本,我也不知道。可能是这题希望使用正则表达式解决吧

欧德奈瑞 发表于 2020-5-21 10:08:06

sunrise085 发表于 2020-5-21 09:35


大佬!我把您的代码输出修改成print(clear_word(str1).lstrip())之后就通过了!非常感谢你!

欧德奈瑞 发表于 2020-5-21 10:09:40

Twilight6 发表于 2020-5-21 09:24


虽然大佬您的代码在我这道题目不知道哪里出了问题,但是还是非常感谢你!

Twilight6 发表于 2020-5-21 10:10:16

欧德奈瑞 发表于 2020-5-21 10:09
虽然大佬您的代码在我这道题目不知道哪里出了问题,但是还是非常感谢你!

import re
string = """#1: welcome @tom to the2 Basic-building..."""

string = re.sub(r'','',string)
print(string)
string = re.sub(r'-','',string)
print(string)
cut_string = string.split(' ')
print(cut_string)
for i in cut_string:
    if i == '#' or i == '@':
      cut_string.remove(i)
    if i[-3:] == '...':
      temp = i[:-3]
      cut_string.insert(cut_string.index(i),temp)
      cut_string.remove(i)
string = ' '.join(cut_string)

Twilight6 发表于 2020-5-21 10:10:49

欧德奈瑞 发表于 2020-5-21 10:09
虽然大佬您的代码在我这道题目不知道哪里出了问题,但是还是非常感谢你!

。。。你要求的是使用正则啊,刚刚写好

sunrise085 发表于 2020-5-21 14:09:21

Twilight6 发表于 2020-5-21 10:10


正则表达式应该是这样的。
你的程序有的是用正则去掉的有的则不是用正则去掉的。。。
import re
def clear_word(string):
    string = re.sub(r'[\d\-]','',string) # 去掉数字和破折号
    string = re.sub(r'\.{3}\B','',string)# 去掉单词末尾的...
    string = re.sub(r'\B(@|#)[^\s]*(\s)?','',string)# 去掉以#或@开头的单词
    return string
string = input()
print(clear_word(string))

Twilight6 发表于 2020-5-21 14:15:34

sunrise085 发表于 2020-5-21 14:09
正则表达式应该是这样的。
你的程序有的是用正则去掉的有的则不是用正则去掉的。。。

emmm 这个我知道,而你上面的全部不是

sunrise085 发表于 2020-5-21 14:35:04

Twilight6 发表于 2020-5-21 14:15
emmm 这个我知道,而你上面的全部不是

因为我之前也没看到要用正则表达式
页: [1] 2
查看完整版本: 字符串的美化 (题目类型:Python字符串与正则表达式)