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 cookbook(数据结构与算法)实现优先级队列的方法示例

本文实例讲述了Python实现优先级队列的方法。分享给大家供大家参考,具体如下: 问题:要实现一个队列,它能够以给定的优先级对元素排序,且每次pop操作时都会返回优先级最高的那个元素;...

python: 判断tuple、list、dict是否为空的方法

Test tuple_test = () assert not tuple_test list_test = [] assert not list_test dict_test...

python解析模块(ConfigParser)使用方法

测试配置文件test.conf内容如下: 复制代码 代码如下:[first]w = 2v: 3c =11-3 [second] sw=4test: hello 测试配置文件中有两个区域,...

初学python的操作难点总结(新手必看篇)

如下所示: 1 在cmd下 盘与盘之间的切换 直接 D或d: 就好 2 查找当前盘或者文件下面的目录 直接 dir 3 想在一个盘下进去一个文件夹,用cd空格目标文件 cd p 4 写文...

python threading模块操作多线程介绍

python是支持多线程的,并且是native的线程。主要是通过thread和threading这两个模块来实现的。thread是比较底层的模块,threading是对thread做了一...