当前位置: 首页 > 编程日记 > 正文

Python maketrans() 方法

描述

Python maketrans() 方法用于给 translate() 方法创建字符映射转换表。

可以只接受一个参数,此时这个参数是个字典类型(暂不研究这种情况)。

对于接受两个参数的最简单的调用方式,第一个参数是字符串,表示需要转换的字符,第二个参数也是字符串,表示转换的目标。两个字符串的长度必须相同,为一一对应的关系。

在Python3中可以有第三个参数,表示要删除的字符,也是字符串。

一般 maketrans() 方法需要配合 translate() 方法一起使用。

注:Python3.4 以后已经不需要从外部 string 模块中来调用 maketrans() 方法了,取而代之的是内建函数: bytearray.maketrans()、bytes.maketrans()、str.maketrans()。

语法

maketrans() 方法语法:

Python3中:

str.maketrans(intab,outtab[,delchars])

Python2中:

import string
string.maketrans(intab,outtab)

参数

  • intab -- 需要转换的字符组成的字符串。
  • outtab -- 转换的目标字符组成的字符串。
  • delchars -- 可选参数,表示要删除的字符组成的字符串。

返回值

返回一个字符映射转换表供 translate() 方法调用。

实例

以下实例展示了使用 maketrans() 方法加 translate() 方法将所有元音字母转换为指定的数字,并删除指定字符:

Pyhon3中:

#!/usr/bin/python3intab = "aeiou"
outtab = "12345"
deltab = "thw"trantab1 = str.maketrans(intab,outtab) # 创建字符映射转换表
trantab2 = str.maketrans(intab,outtab,deltab) #创建字符映射转换表,并删除指定字符test = "this is string example....wow!!!"print(test.translate(trantab1))
print(test.translate(trantab2))

以上实例输出结果如下:

th3s 3s str3ng 2x1mpl2....w4w!!!
3s 3s sr3ng 2x1mpl2....4!!!

Python2中:

#!/usr/bin/python
# -*- coding: UTF-8 -*-import string   # 导入string模块intab = "aeiou"
outtab = "12345"
deltab = "thw"trantab = string.maketrans(intab,outtab) # 创建字符映射转换表test = "this is string example....wow!!!";print test.translate(trantab);
print test.translate(trantab,deltab); # Python2中,删除指定字符在 translate() 方法中

以上实例输出结果如下:

th3s 3s str3ng 2x1mpl2....w4w!!!
3s 3s sr3ng 2x1mpl2....4!!!

转载于:https://www.cnblogs.com/wushuaishuai/p/7687074.html

相关文章:

二:serverless网站数据库操作

操作数据库API文档 第一篇,搭建一套免费的serverless网站 一,创建集合,进入控制台 登录腾讯云后台 打开云开发控制台 可以新建集合,新建集合后点击集合名称即可批量导入导出数据库集合的数据,还可以设置数据库访问的…

我希望支持JavaScript GraphQL实现的API

The GraphQL schema language is great! It is certainly the best way to communicate anything about a GraphQL service. No wonder all documentations now use it!GraphQL 模式语言很棒! 当然,这是传达与GraphQL服务有关的任何东西的最佳方法。 难怪…

2_Selenium对象识别

1 准备工作 firebug和firepath我们使用xpath进行元素定位,所以需要安装firefox的两个插件,帮助编写xpath html知识在编写xpath的时候,需要查看html代码,所以需要理解html知识,可以到http://www.w3school.com.cn/去自学…

Mac OS Terminal Commands

转自 : http://www.renfei.org/blog/mac-os-x-terminal-101.html Mac OS X Terminal 101:终端使用初级教程 July 29, 2012 / 编程指南最近学习苹果认证的《Mac OS X Support Essentials》教程,看到 Command Line 一节有很多实用的知识&#x…

封装一个计时器,记录页面的停留时间

在页面加载时初始化计时器&#xff0c;页面结束时取值。 效果如图&#xff1a; 下面是uniapp的代码示例&#xff1a; <template><view class"aaaacc"><view class"aaa">{{time}}</view></view> </template><scri…

ess用户名和密码_陈ess洁如何从摄影系学生转变为成功的自由职业者和内容创作者(播客)...

ess用户名和密码This week, for our last podcast episode of 2019, I got to chat with freelancer and content creator Jessica Chan - known as CoderCoder on social media - about how she got into tech and started her educational website and YouTube channel.本周&…

安装wxpython——python程序GUI图形界面使用

一、本机使用python版本 1、本机使用python版本为3.6.2&#xff0c;windows系统。 二、安装步骤 1、开始-运行-cmd 2、python3.x 需要进入python安装位置下是pip目录&#xff0c;再执行pip 命令 其中&#xff1a;python F:\Users\Administrator\AppData\Local\Programs\Python\…

ASP.NET全球化与本地化 c#多国语言的支持 (项目支持多国语言的开发)

ASP.NET 2.0及以上的开发平台&#xff0c;为全球化本地化应用程序提供了工具&#xff0c;而且实现起来非常简单。以下内容是使用c#&#xff0c;按照帮助一步步做的&#xff0c;将为初学者提供详细的实现步骤。 一 几个必要概念 &#xff08;一&#xff09; 支持全球化  由于…

promise 和 async await区别

什么是Async/Await? async/await是写异步代码的新方式&#xff0c;以前的方法有回调函数和Promise。   async/await是基于Promise实现的&#xff0c;它不能用于普通的回调函数。   async/await与Promise一样&#xff0c;是非阻塞的。   async/await使得异步代码看起来像…

mac 制作usb启动盘_如何使用Mac制作Windows 10 USB-从Mac终端构建可启动的ISO

mac 制作usb启动盘Most new PCs dont come with DVD drives anymore. So it can be a pain to install Windows on a new computer.大多数新PC不再附带DVD驱动器。 因此&#xff0c;在新计算机上安装Windows可能会很痛苦。 Luckily, Microsoft makes a tool that you can use …

作业05-继承、多态、抽象类与接口

1. 本周学习总结 1.1 写出你认为本周学习中比较重要的知识点关键词 接口&#xff0c;Comparable&#xff0c;Comparator 1.2 尝试使用思维导图将这些关键词组织起来。 1.3 可选&#xff1a;使用常规方法总结其他上课内容。 1.接口特点&#xff1a;一个公开的界面 有统一定义的规…

炫彩流光按钮 html+css

话不多&#xff0c;先上效果&#xff1a; 简介&#xff1a; 用基础css做一个有一点炫酷的流光按钮&#xff0c;不止按钮&#xff0c;只要是盒子就行。 具体步骤&#xff1a; 1.先定义一个盒子当做按钮&#xff0c;如我就用a标签&#xff1a; <body><a href"#…

SQL取上一条, 下一条记录方法

如果我们需要取id为3的前后的1条记录. 就可以用以下方法 取上一条记录: select * from 表名 where id<3 order by id desc limit 1取下一条记录: select * from 表名 where id>3 order by id asc limit 1大小于比较 order limit 转载于:https://www.cnblogs.com/…

react中使用构建缓存_通过构建海滩度假胜地网站,了解如何使用React,Contentful和Netlify...

react中使用构建缓存In this full course from John Smilga you will learn React by building a beach resort website project. The project uses React router for routing, React context API for state management, Contentful headless CMS for data management, and Net…

R语言实战 - 基本统计分析(2)- 频数表和列联表

数据&#xff1a; > library(vcd) > head(Arthritis)ID Treatment Sex Age Improved 1 57 Treated Male 27 Some 2 46 Treated Male 29 None 3 77 Treated Male 30 None 4 17 Treated Male 32 Marked 5 36 Treated Male 46 Marked 6 23 …

随机位置显示图片不重叠前端实现详细讲解附效果图,代码可直接使用

目录先看看效果图所要实现的功能看看代码js代码讲解&#xff1a;下面看看完整代码吧小程序版本完整代码VUE版本的代码&#xff1a;先看看效果图 所要实现的功能 在页面上随机的位置显示随机大小的图片&#xff0c;并且每个图片不能重叠&#xff0c;完整实现代码。 看看代码 …

MVC缓存OutPutCache学习笔记 (一) 参数配置

OutPutCache 参数详解 Duration : 缓存时间&#xff0c;以秒为单位&#xff0c;这个除非你的LocationNone&#xff0c;可以不添加此属性&#xff0c;其余时候都是必须的。 Location : 缓存放置的位置; 该值为枚举值: None: 当被设置为None时&#xff0c;其余的任何设置将不起作…

在Mac上控制Alt Delete-如何在Macbook上打开任务管理器

It happens to the best of us: were working away on some important project, and our trusty computer freezes. Or rather, a program were in just stops responding. So what do you do?这对我们最好的人来说是偶然的&#xff1a;我们正在做一些重要的项目&#xff0c;而…

HEW MAP文件使用

参考资料转载于:https://www.cnblogs.com/iluzhiyong/p/5145396.html

算法导论九章 答案

http://blog.csdn.net/z84616995z/article/details/18840823?reload 9.3-8题&#xff1a; http://blog.csdn.net/z84616995z/article/details/18938181 9.3-9题&#xff1a; http://blog.csdn.net/z84616995z/article/details/18889535转载于:https://www.cnblogs.com/sa51718…

小程序实现瀑布流,获取图片高度分成两组数据的函数封装代码

把一个数组根据数组内部的图片的高度&#xff0c;拆分成两个数组&#xff0c;组成瀑布流数据的方法。 使用方式&#xff1a; async created() {var arr this.$mock.sssdata.data.lists;//arr 是一个数组&#xff0c;里面包含若干对象&#xff0c;对象里面有图片var myArr a…

实现线程哪种方法更好_实施数据以实现更好的用户体验设计的4种方法

实现线程哪种方法更好Gone are the days when design used to rely mainly on the color palettes and the creativity of the designer. In the rapidly expanding technological world of today, it is essential to work across departments to enhance the screen experien…

String比较.equals

首先定义四个变量str public class SIzhui {public static void main(String[] args) {String str1"wang";String str2"li";String str3"wang";String str4new String("wang");} } 然后进行比较 public class SIzhui {public static …

mac下mysql5.7.10密码问题

mysql5.7.10刚安装好&#xff0c;会生成一个随机密码。 如果没记住这个随机密码&#xff0c;那么到mysql/bin/下执行mysql_secure_installation命令 按照提示重置密码和其他选项。 ps&#xff1a;找了一下午终于找到方法了&#xff01;&#xff01;&#xff01; 转载于:https:/…

Error: Cannot find module ‘express‘

解决方案&#xff1a;把node_module整个文件夹删掉,然后npm clean cache,看下package.json里有没有express的依赖项,有的话直接npm install,没有的话 npm install express --save

Observables简介以及它们与Promise有何不同

‘Observables’, ‘Observables’, ‘Observables’...Yes! Today, we will talk about this often discussed word of the market. Well also learn how they are different from Promises (havent heard about Promises? Not to worry! You will know more soon). Let’s s…

Spring Boot项目错误:Error parsing lifecycle processing instructions

pom.xml文件错误&#xff1a;Error parsing lifecycle processing instructions 解决方法&#xff1a;清空.m2/repository下的所有依赖文件&#xff0c;重新下载即可解决该问题。转载于:https://www.cnblogs.com/EasonJim/p/7724683.html

oracle执行计划连接方式

嵌套循环&#xff08;Nested Loops &#xff08;NL&#xff09;&#xff09;假如有A、B两张表进行嵌套循环连接&#xff0c;那么Oracle会首先从A表中提取一条记录&#xff0c;然后去B表中查找相应的匹配记录&#xff0c;如果有的话&#xff0c;就把该条记录的信息推到等待返回的…

大转盘完整源码附效果图,可以下载直接用

本转盘实现功能&#xff0c;可以动态配置奖品和转盘相关的任何图片&#xff0c;可以灵活配置使用。是基于 uni-app 实现的。可以在小程序或者H5&#xff0c;各端兼容使用。 效果图&#xff1a;因为GIF图的掉帧&#xff0c;所以显示抽奖的转动速度慢&#xff0c;实际上转动比较…

使用FortJs使用现代JavaScript开发Node.js

介绍 (Introduction) Nodejs gives you the power to write server side code using JavaScript. In fact, it is very easy and fast to create a web server using Nodejs. There are several frameworks available on Node package manager which makes the development eve…