Python 使用此代码用随机符号替换坏单词时遇到问题
我正在学习Python,并试图制作一个程序,要求用户输入文本,并用一组随机符号替换任何坏单词,这些符号来自定义的字符符号元组,与用户输入的坏单词长度匹配。 我不断地遇到这个错误,我不知道为什么: 回溯(最后一次调用):文件“replace\u bad\u words.py”,第行 30,在 rchars=sample(chars,y)文件“/usr/lib/python3.5/random.py”,第315行,在sample中 提升值错误(“样本大于总体”)值错误:样本大于总体 我是新来的,希望能从这个伟大的社区得到一些反馈。对于其他社区帮助调试python新手的任何建议也将不胜感激。当然,任何关于更好、更高效的代码的提示,或者更好的编码风格,都是非常好的。谢谢Python 使用此代码用随机符号替换坏单词时遇到问题,python,python-3.x,Python,Python 3.x,我正在学习Python,并试图制作一个程序,要求用户输入文本,并用一组随机符号替换任何坏单词,这些符号来自定义的字符符号元组,与用户输入的坏单词长度匹配。 我不断地遇到这个错误,我不知道为什么: 回溯(最后一次调用):文件“replace\u bad\u words.py”,第行 30,在 rchars=sample(chars,y)文件“/usr/lib/python3.5/random.py”,第315行,在sample中 提升值错误(“样本大于总体”)值错误:样本大于总体 我是新来的,希望
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
sample(chars,y)
从chars
中提取y
不同的元素,并将其洗牌并返回结果列表。因此,如果y>len(chars)
(这里就是这种情况),函数将引发ValueError
但是,如果您在代码中确保任何“坏单词”的长度小于或等于
字符的长度,这将起作用。例如,我用以下内容更改了您对chars
的定义:
chars=(“@'、“#'、“$”、“%”、“&'、“!”、“@'、“#'、“$”、“%”、“&'、“!”)
该测试给出:
Write some words about yourself: hello badword1 this is badword2.
hello %@$#@&$& this is %$$%!!@&
random
软件包的另一个更适合您需要的功能是choices
,它可以从总体中随机选择k
项并返回结果列表。例如:
>random.chooses((“@”、“#”、“美元”、“百分比”、“和”、“!”),k=10)
['$', '&', '!', '&', '&', '&', '&', '!', '&', '!']
您可以看到,相同的项目可能会出现在结果列表中。有点离题,但我建议您使用set()而不是list,因为在大型列表中,查找速度会非常慢。此外,您不需要单独列出带有标点符号的坏单词,只需在检查坏单词之前去掉标点符号即可。使用而不是生成坏单词替换。这就是您看到的错误消息的原因
错误消息声明“样本大于总体”。调用sample(chars,y)
和y>len(chars)
时会发生此错误sample()
从没有替换的字符中随机选择,这意味着替换的长度最多可以是len(字符)
longchoices()
取而代之的是选择替换,这意味着您可以获得任意长的替换
from random import choices
rchars = choices(chars, k=y)
非常感谢你!我知道这是一件很简单的事情,但我想不出来。你让我开心非常感谢,这正是我想要的!非常感谢。我试试看。使用strip方法?我想我要做的是打印出尽可能接近用户输入的内容,但用符号替换不好的单词。所以如果我去掉了标点符号,我想知道如何在打印之前把它放回去?>用剥离法?是的,脱衣舞就行了。好的,然后用标点符号保留原单词,计算单词前后标点符号的数量,然后去掉标点符号。如果是一个坏单词,则用所需的替换符号替换除标点符号以外的所有符号(因为标点符号前后都有计数,所以很容易)。