英文输入法

一、题目

主管期望你来实现英文输入法单词联想功能。
需求如下:

依据用户输入的单词前缀,从已输入的英文语句中联想出用户想输入的单词,按字典序输出联想到的单词序列,
如果联想不到,请输出用户输入的单词前缀。
注意:

英文单词联想时,区分大小写
缩略形式如”don’t”,判定为两个单词,”don”和”t”
输出的单词序列,不能有重复单词,且只能是英文单词,不能有标点符号

输入描述
输入为两行。

首行输入一段由英文单词word和标点符号组成的语句str;

接下来一行为一个英文单词前缀pre。

0 < word.length() <= 20
0 < str.length <= 10000
0 < pre <= 20
输出描述
输出符合要求的单词序列或单词前缀,存在多个时,单词之间以单个空格分割

示例1
输入

I love you
He
1
2
输出

He

示例2
输入

The furthest distance in the world, Is not between life and death, But when I stand in front of you, Yet you don’t know that I love you.
f
1
2
输出

front furthest

二、代码

import string

sentence = input() # 输入一段由英文单词word和标点符号组成的语句
prefix = input() # 输入一个英文单词前缀

# 将标点符号替换为空格
sentence = sentence.translate(str.maketrans(string.punctuation, ' '*len(string.punctuation)))

word_set = sorted(set(sentence.split()))
print(word_set)

ans = ''
for word in word_set:
    if word.startswith(prefix):
        ans += word + ' '

if ans:
    print(ans)
else:
    print(prefix)

说明:

string.punctuation

string.punctuation 是 Python 标准库 string 模块中预定义的一个常量,它包含所有常见的 ASCII 标点符号,是一个长度为 32 的字符串。

'!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
str.maketrans(string.punctuation, ' '*len(string.punctuation))

三、maketrans 方法详解

str.maketrans() 是 Python 字符串类的一个静态方法,用于创建字符映射转换表,通常与 translate() 方法配合使用进行字符替换。

1. 基本语法

str.maketrans(x[, y[, z]])

参数说明:

  • x:必需,如果只有一个参数,必须是字典;如果有两个或三个参数,必须是等长的字符串
  • y:可选,与 x 等长的字符串,表示要替换成的字符
  • z:可选,字符串,表示要删除的字符

2. 三种使用方式

方式一:字典映射
# 创建字符映射字典
trans_dict = {'a': '1', 'b': '2', 'c': '3'}
table = str.maketrans(trans_dict)

text = "abc def"
result = text.translate(table)
print(result)  # 输出: "123 def"
方式二:两个字符串参数(等长替换)
# 创建替换映射:a→1, b→2, c→3
table = str.maketrans('abc', '123')

text = "apple banana cherry"
result = text.translate(table)
print(result)  # 输出: "1pple 21n1n2 3herry"
方式三:三个参数(替换+删除)
# 创建映射:a→1, b→2,同时删除所有标点
table = str.maketrans('ab', '12', ',.!?')

text = "a, b! c."
result = text.translate(table)
print(result)  # 输出: "1 2  c"

3. 在题目中的应用

在本题的代码中:

str.maketrans(string.punctuation, ' '*len(string.punctuation))

这行代码的作用是:

  1. string.punctuation:包含所有 ASCII 标点符号的字符串
  2. ' '*len(string.punctuation):创建与标点符号数量相等的空格字符串
  3. 创建了一个映射表:将每个标点符号映射为一个空格
  4. 配合 translate() 方法,将所有标点符号替换为空格

4. 实际应用场景

场景一:清理文本
import string

text = "Hello, World! How are you?"
# 移除所有标点
clean_text = text.translate(str.maketrans('', '', string.punctuation))
print(clean_text)  # 输出: "Hello World How are you"
场景二:字符加密
# 简单的凯撒密码(偏移3位)
original = "abcdefghijklmnopqrstuvwxyz"
shifted = "defghijklmnopqrstuvwxyzabc"

table = str.maketrans(original, shifted)
message = "hello world"
encrypted = message.translate(table)
print(encrypted)  # 输出: "khoor zruog"
场景三:批量替换
# 替换特定字符
table = str.maketrans('aeiou', '12345')
text = "The quick brown fox jumps"
result = text.translate(table)
print(result)  # 输出: "Th2 q5432 br4wn f4x j5mps"

5. 注意事项

  1. 性能优势translate() + maketrans() 比循环使用 replace() 更高效,特别是处理大量文本时
  2. Unicode 支持maketrans() 支持 Unicode 字符
  3. 映射关系:如果映射字典中有重复键,后面的值会覆盖前面的值
  4. 删除操作:第三个参数指定的字符会被直接删除,不会被替换

6. 与正则表达式的对比

方法适用场景性能复杂度
translate() + maketrans()简单的字符对字符替换/删除
str.replace()简单的字符串替换
re.sub()复杂的模式匹配替换

在本题中,使用 maketrans() 将标点替换为空格是最简洁高效的方法。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值