#读取长字符串 f = open(r'D:\test\test.txt',"r",encoding="UTF-8") b = f.read() #字符串转列表 lst = list(b) #取逗号,句号(列表) lst = list(filter(lambda x:x != ".",lst)) **重点 列表中取逗号 句号 lst = list(filter(lambda x:x != ",",lst)) #列表转字符串 words = ''.join(lst) #分割单词 - 返回单词列表 lst = words.split(' ') #去除列表空项 lst = list(filter(lambda x:x != '',lst )) for word in lst: if word[::-1] in lst: print(word,word[::-1])
两个单词,如果其中一个是另一个的反向序列,则称它们为”反向对“。 比如,dog,反过来是god;eye,反过来还是eye. 编写一个程序找出字符串中的全部反向对。
最新推荐文章于 2023-08-08 01:46:39 发布