Python给定一个句子倒序输出单词以及字母的方法

yipeiwu_com6年前Python基础

如下所示:

#!/usr/bin/python
# -*- coding: utf-8 -*-
def rever(sentence):
  newwords = []
  words = sentence.split()
  words.reverse()
  space = ' '#单词之间一个间隔
  for word in words:
    newword = []
    new = ''#单词的字母间无间隔
    l = len(word)
    for i in range(l):
      newword.append(word[l-i-1])
    neww = new.join(newword)
    newwords.append(neww)
  print space.join(newwords)
rever('what\'s your problem man ?')

运行结果:

? nam melborp ruoy s'tahw

以上这篇Python给定一个句子倒序输出单词以及字母的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

django使用haystack调用Elasticsearch实现索引搜索

django使用haystack调用Elasticsearch实现索引搜索

前言: 在做一个商城项目的时候,需要实现商品搜索功能。 说到搜索,第一时间想到的是数据库的 select * from tb_sku where name like %苹果手机% 或者...

用Python中的wxPython实现最基本的浏览器功能

通常,大多数应用程序通过保持 HTML 简单来解决大多数浏览器问题 ― 或者说,根据最低共同特性来编写。然而,即便如此,也仍然存在字体和布局的问题,发行新浏览器和升级现有浏览器时,也免不...

Python库urllib与urllib2主要区别分析

作为一个Python菜鸟,之前一直懵懂于urllib和urllib2,以为2是1的升级版。今天看到老外写的一篇《Python: difference between urllib and...

python判断列表的连续数字范围并分块的方法

情况一:列表中的数字是连续数字(从小到大) from itertools import groupby lst = [1, 2, 3, 5, 6, 7, 8, 11, 12, 13...

python 读取视频,处理后,实时计算帧数fps的方法

实时计算每秒的帧数 cap = cv2.VideoCapture("DJI_0008.MOV") #cap = cv2.VideoCapture(0) # Define the...