python转换字符串为摩尔斯电码的方法

yipeiwu_com6年前Python基础

本文实例讲述了python转换字符串为摩尔斯电码的方法。分享给大家供大家参考。具体实现方法如下:

chars = ",.0123456789?abcdefghijklmnopqrstuvwxyz"
codes = """--..-- .-.-.- ----- .---- ..--- ...-- ....- ..... -.... --... ---..
      ----. ..--.. .- -... -.-. -... . ..-. --. .... .. .--- -.- .-.. --
      -. --- .--. --.- .-. ... - ..- ...- .-- -..- -.-- --.."""
keys = dict(zip(chars, codes.split()))
def char2morse(char):
  return keys.get(char.lower(), char)
print ' '.join(char2morse(c) for c in 'SOS')

运行结果如下:
... --- ...

希望本文所述对大家的Python程序设计有所帮助。

相关文章

python操作sqlite的CRUD实例分析

本文实例讲述了python操作sqlite的CRUD实现方法。分享给大家供大家参考。具体如下: import sqlite3 as db conn = db.connect('myt...

python opencv图片编码为h264文件的实例

python部分 #!/usr/bin/env Python # coding=utf-8 from ctypes import * from PyQt5.QtCore impo...

Python装饰器知识点补充

首先回顾一下关于Python装饰器以及装饰器模式 补全 根据Java实现装饰器模式的,我们可以写下面一段代码: import logging def use_logging(f...

python 截取 取出一部分的字符串方法

下面是split截取获得 >>> str = 'http://manualfile.s3.amazonaws.com/pdf/gti-chis-1-user-9fb...

对python中的xlsxwriter库简单分析

一、xlsxwriter 基本用法,创建 xlsx 文件并添加数据 官方文档:http://xlsxwriter.readthedocs.org/ xlsxwriter 可以操作 xls...