mystr = "illusion never changed into something real wide awake and i can see the perfect sky ees torn you are a little late I'm already torn"
def reverse(word):
letter = list(word)
length = len(letter)
y = []
for x,w in enumerate(letter):
y.append("".join(letter[(length-1)-x]))
return("".join(yy for yy in y))
words = mystr.split()
for word in words:
if (reverse(word)) in words and len(word) > 1: # len(word)>1 is for ignoring a word that contains only one letter, e.g. 'I' and 'a'.
print ("'" + word + "' is the reverse of '" + reverse(word) + "'")
输出:
^{pr2}$
您也可以尝试@Nuhman建议的更简单的方法。在
mystr = "illusion never changed into something real wide awake and i can see the perfect sky ees torn you are a little late I'm already torn"
words = mystr.split()
for word in words:
if word[::-1] in words and len(word) > 1:
print ("'" + word + "' is the reverse of '" + reverse(word) + "'")
hello = "illusion never changed into something real wide awake and i can see the perfect sky ees torn you are a little late I'm already torn"
words = hello.split(" ")
reverse_words = []
for word in words:
if word[::-1] in words and len(word)>1 and word[::-1] not in reverse_words:
reverse_words.append(word)
print(reverse_words)
你可以试试这个。在
输出:
^{pr2}$您也可以尝试@Nuhman建议的更简单的方法。在
输出:
^{pr2}$使用
word[::-1]
反转单词,如果单词列表中存在反转,则将其另存为list
。在输出:
^{pr2}$相关问题 更多 >
编程相关推荐