我创建了一个列表characters = list(original.lower())
来列出四个字母单词的输入。这会将输入分解为sepearte字符并使它们全部小写。
在下面的函数中,我需要调用每个被分隔的字母,并用5个ascii字符中的1个替换它们! %*#@
我已经将列表创建为名为 - obfuscate = ["!", "%", "*", "#", "@"]
的var,现在需要但不知道如何引入每个字符并将随机符号应用于输入的每个字母。
original = input("Enter a 4-letter word: ")
letters = isolate_letters(original)
obsfucate_letters(letters)
obfuscated = recompose_obfuscated_letters(letters)
print("Original: ", original)
print("Obfuscated: ", obfuscated)
def isolate_letters(original):
characters = list(original.lower())
print (characters)
return characters
def obsfucate_letters(original):
import random
obfuscate = ["!", "%", "*", "#", "@"]
print (random.choice(obfuscate))
编辑:
def obfuscate_letters(word):
import random
new_word = ''
for char in word:
if random.random() < 0.9:
new_word += random.choice(['!', '%', '&', '#', '@'])
else:
new_word += char
letters = new_word
print (letters)
def recompose_obfuscated_letters(letters):
obfuscated = ''.join(letters)
return obfuscated
这是我的最后两个函数,我无法返回字母变量:
这是对dddd的回应:
!@%!
Original: dddd
Obfuscated: dddd
乱码的顶线是我需要在Obfuscated位旁边; /
答案 0 :(得分:1)
你可以创建一个字典,将字母映射到其混淆的字符
import random
def encode(word, symbols):
obfuscate = list(symbols)
random.shuffle(obfuscate)
d = dict(zip(word, obfuscate))
return ''.join(d[i] for i in word)
>>> obfuscate = ["!", "%", "*", "#", "@"]
>>> word = 'test'
>>> encode(word, obfuscate)
'#%*#'
在编码功能中,第三行创建以下形式的字典
{'s': '*',
't': '#',
'e': '%'}
由于我在zip
之前对列表进行洗牌,因此地图将随机配对。请参阅以下几个测试呼叫
>>> encode(word, obfuscate)
'%!#%'
>>> encode(word, obfuscate)
'@#%@'
>>> encode(word, obfuscate)
'@#*@'
>>> encode(word, obfuscate)
'%!*%'
>>> encode(word, obfuscate)
'@*%@'
答案 1 :(得分:1)
如果您不需要重新创建单词后者。这是一个单行解决方案。
import random
def obfuscate(word):
return ''.join([random.choice(["!", "%", "*", "#", "@"]) for char in word])
Explantion:
[... for ... in ...]是list comprehension,它使用random.choice()生成随机字符列表,然后''.join(),连接所有在单个字符串中生成的字符。
编辑:
如果'word'是用户输入(四个字母的单词)怎么办?
user_input = raw_input("Enter a 4-letter word: ")
if len(user_input) == 4:
print obfuscate(user_input)
有没有办法让单词中有一个字母有10%的可能性被单独留下而不会被更改?
为此,列表理解不起作用(据我所知),但你仍然可以在for循环中进行。
def obsfucate(word):
new_word = ''
for char in word:
if random.random() > 0.1:
new_word += random.choice(["!", "%", "*", "#", "@"])
else:
new_word += char
return new_word
输出:
Enter a 4-letter word: Damn
D%*#
EDIT2:
实际上你可以使用列表推导!
def obsfucate(word):
return ''.join([random.choice(["!", "%", "*", "#", "@"]) if random.random() > 0.1 else char for char in word])
但对我来说它有点乱。
编辑3:
完整代码应该是这样的:
import random
def obfuscate(word):
new_word = ''
for char in word:
if random.random() > 0.1:
new_word += random.choice(["!", "%", "*", "#", "@"])
else:
new_word += char
return new_word
user_input = raw_input("Enter a 4-letter word: ")
if len(user_input) == 4:
print "Original:", user_input
print "Obfuscate:", obfuscate(user_input)
输出:
Enter a 4-letter word: Damn
Original: Damn
Obfuscate: D@m%
如果您想按原样使用代码:
import random
def isolate_letters(original):
characters = list(original.lower())
return characters
def obsfucate(word):
return [random.choice(["!", "%", "*", "#", "@"]) if random.random() > 0.1 else char for char in word]
def recompose_obfuscated_letters(letters):
obfuscated = ''.join(letters)
return obfuscated
original = input("Enter a 4-letter word: ")
letters = isolate_letters(original)
obsfucate_letters = obsfucate(letters)
obfuscated = recompose_obfuscated_letters(obsfucate_letters)
print("Original:", original)
print("Obfuscated:", obfuscated)
输出:
Enter a 4-letter word: Damn
Original: Damn
Obfuscated: d!!%
答案 2 :(得分:0)
这是我的整个代码,您可以在代码摘录中使用。
from __future__ import print_function
def isolate_letters(s):
return [c for c in s]
def obfuscate_letters(loc):
from random import shuffle
funny = ['!', '%', '*', '#', '@']
# shuffle the list of funny characters
shuffle(funny)
# create a dictionary
d = {c:f for f,c in zip(funny,set(loc))}
# change in place the list of characters
for i, c in enumerate(loc):
loc[i] = d[c]
def recompose_obfuscated_letters(looc):
return "".join(looc)
# here it is a copy and past from your OP (except for the spellung of obfuscate...)
original = 'funk'
letters = isolate_letters(original)
obfuscate_letters(letters)
obfuscated = recompose_obfuscated_letters(letters)
print('Original: "%s"' % original)
print('Obfuscated: "%s"' % obfuscated)
我的输出是
Original: "funk"
Obfuscated: "*#%@"
Key正在使用shuffle
模块中的random
函数。 shuffle
随机更改列表中项目的位置,以便当我按顺序访问funny
中的条款时,我实际上会随机访问它们...
下一期,我使用set()
在输入词中找到唯一字符,然后构建映射,从真实字符到搞笑字符。
当然,最后一步是浏览原始列表并使用我们在上面定义两行的映射逐个更改所有项目。
(最后一部分,修改原始列表,遵循OP如何编码她/他的例子)
答案 3 :(得分:0)
一个新答案,因为OP基本上有一个新问题
def obfuscate_letters(s,funny=[c for c in "!!!%%%&&&###@@@ "]):
from random import shuffle
shuffle(funny)
for i, c in enumerate(s):
s[i] = funny[i] if funny[i] != ' ' else s[i]
(是的,默认参数是可变的并且在每次调用时都会更改)