使用此代码将坏单词替换为随机符号时遇到问题



我正在学习Python,并试图制作一个程序,要求用户输入文本,并用定义的字符符号元组中的一组随机符号替换任何坏单词,该符号与用户输入的坏单词的长度相匹配。我一直收到这个错误,我不知道为什么:

Traceback(最近调用last(:文件;replace_bad_words.py";,线30,英寸rchars=sample(chars,y(File"/usr/lib/python3.5/arandom.py";,第315行,样本中提升值错误("样本大于总体"(值错误:样本大于总体

这里是新的,希望这个伟大的社区能提供一些反馈。对于其他社区帮助调试python新手的任何建议也将不胜感激。当然,任何关于更好、更高效的代码来做我正在尝试做的事情,或者更好的编码风格的技巧都会很棒。谢谢

from random import sample
# Make the bad word lists
bwlist = ['badword1', 'badword2', 'badword3', 'badword4', 'badword5', 'badword6', 'badword7', 'badword8']
bw2 = [wd + ',' for wd in bwlist]
bw3 = [wd + '.' for wd in bwlist]
bw4 = [wd + '!' for wd in bwlist]
bw5 = [wd + '?' for wd in bwlist]
chars = ('@', '#', '$', '%', '&', '!')
# Ask for some text input
aa = input('Write some words about yourself: ')
# Convert the user's text into a list of words
# Create a copy of the list
bb = aa.split()
cc = bb.copy()
# Create an empty string for joining random characters to replace the bad words
nsp = ''
# Loop through the list of words and store the index and length of the bad word
for i in bb :
if i in bwlist :
x = bb.index(i)
y = len(i)
# Produce a list of random characters matching length of the bad word
rchars = sample(chars, y)
# Replace the bad word with a string of random characters in the copy of the list
cc[x] = nsp.join(rchars)
# Same as above, but removes punctuation from bad words
elif i in bw2 or i in bw3 or i in bw4 or i in bw5 :
x = bb.index(i)
y = len(i) - 1
rchars = sample(chars, y)
cc[x] = nsp.join(rchars)
# Convert the list of user text back to a string with bad words replaced and print
sp = ' '
edited_user_inp = sp.join(cc)
print(edited_user_inp)

您不希望在此处使用random.sample。CCD_ 2从CCD_ 4获取CCD_。因此,如果是y > len(chars),也就是这里的情况,函数将引发一个ValueError
但是,如果您在代码中确保任何";坏词";小于或等于chars的长度,这将起作用。例如,我将您对chars的定义更改为:

chars = ('@', '#', '$', '%', '&', '!', '@', '#', '$', '%', '&', '!')

该测试给出:

Write some words about yourself: hello badword1 this is badword2.
hello %@$#@&$& this is %$$%!!@&

random包的另一个似乎更适合您需求的功能是choices,它可以从总体中随机选择k项目并返回结果列表。例如:

>>> random.choices(('@', '#', '$', '%', '&', '!'), k=10)
['$', '&', '!', '&', '&', '&', '&', '!', '&', '!']

您可以在此处看到,相同的项目可能会出现在结果列表中。

有点偏离主题,但我建议您使用set((而不是list,因为在大列表上查找会非常慢。此外,您不需要单独列出带有标点符号的坏单词,只需在检查坏单词之前去掉标点符号即可。

使用random.choices((而不是random.sample((来生成坏词替换。这就是您看到的错误消息的原因。

错误消息指出"样本大于总体"。调用sample(chars, y)y > len(chars)时会发生此错误。sample()从没有替换的字符中随机选择,这意味着替换的长度最多可以是len(chars)长。choices()选择替换,这意味着您可以获得任意长的替换。

from random import choices
rchars = choices(chars, k=y)

最新更新