意义:简单实现摩斯码的破译和生成

代码:

#-*- coding: UTF-8 -*-

__author__ = ''
__date__ = '2016/2/2' import pprint
import re chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
codes = """.- -... -.-. -.. . ..-. --. .... .. .--- -.- .-..
-- -. --- .--. --.- .-. ... - ..- ...- .-- -..- -.-- --..
.---- ..--- ...-- ....- ..... -.... --... ---.. ----. -----""" dd = dict(zip(chars.lower(),codes.split()))
DD = dict(zip(codes.split(),chars.lower())) #pprint.pprint(DD) def chars2morse(char):
return dd.get(char.lower(),' ') def morse2chars(morse):
return DD.get(morse,' ') while True:
str = raw_input()
x = str.split(' ')
ccc = ''.join(x)
if re.match('^[0-9a-zA-Z]+$',ccc):
print ' '.join(chars2morse(c) for c in ccc)
else:
cc = str.split()
print ' '.join(morse2chars(c) for c in cc)

运行结果:

知识点:

split()

意义:通过指定分割符对字符串进行切片

语法:

str.split(str="", num=string.count(str))
  • 参数str:分隔符,默认为空格
  • 参数num:分割次数

返回值:返回分割后的字符串列表

实例:

 In[2]: str = "I Love Python!"
In[3]: str.split()
Out[3]: ['I', 'Love', 'Python!']
In[4]: str.split(' ',1)
Out[4]: ['I', 'Love Python!']

lower()

意义:转换字符串中所有大写字符为小写

语法:

str.lower()

返回值:返回将字符串中所有大写字符转换为小写后生成的字符串

实例:

 In[2]: str = "I Love Python!"
In[5]: str.lower()
Out[5]: 'i love python!'

zip()

意义:Python内建函数,它接受一系列可迭代的对象作为参数,将对象中对应的元素打包成一个个tuple(元组),然后返回由这些tuples组成的 list(列表)。若传入参数的长度不等,则返回list的长度和参数中长度最短的对象相同

语法:

zip([iterable, ...])
  • 参数:任意多个列表

返回值:有元祖组成的列表

实例:

 In[6]: a = [1,2,3]
In[7]: b = [4,5,6]
In[8]: c = [7,8,9,0]
In[9]: zip(a,b,c)
Out[9]: [(1, 4, 7), (2, 5, 8), (3, 6, 9)]

利用*号操作符,可以将list unzip(解压)

 In[11]: d = [[1,2,3],[4,5,6],[7,8,9]]
In[12]: zip(*d)
Out[12]: [(1, 4, 7), (2, 5, 8), (3, 6, 9)]
In[13]: e = [(1, 4, 7), (2, 5, 8), (3, 6, 9)]
In[14]: zip(*e)
Out[14]: [(1, 2, 3), (4, 5, 6), (7, 8, 9)]

dict()

意义:创建新字典

语法:

dict(iterable, **kwarg)

返回值:返回一个新字典

实例:

 #键值对方式构造字典
In[17]: dict(a=1,b=2,c=3)
Out[17]: {'a': 1, 'b': 2, 'c': 3}
#映射函数方式构造字典
In[18]: dict(zip(['a','b','c'],[1,2,3]))
Out[18]: {'a': 1, 'b': 2, 'c': 3}
#可迭代对象方式构造字典
In[19]: dict([('a',1),('b',2),('c',3)])
Out[19]: {'a': 1, 'b': 2, 'c': 3}

get()

意义:返回字典中指定键的值,如果值不在字典中返回默认值

语法:

dict.get(key, default=None)
  • 参数key:字典中要查找的键
  • default:如果指定键的值不存在时,返回该默认值值

返回值:字典中指定键的值,如果值不在字典中返回默认值None

实例:

 In[23]:  d = {'a': 1, 'b': 2, 'c': 3}
In[24]: d.get('a')
Out[24]: 1
In[25]: d.get('d','Not Found!')
Out[25]: 'Not Found!'

最新文章

  1. 在表单中元素的onchange事件的兼容性问题
  2. ffmpeg-20160522-git-bin
  3. 【POJ】3071 Football
  4. XSS检测工具 X5S/fiddler
  5. Windows命令行提取日期时间
  6. MySQL: InnoDB 还是 MyISAM?
  7. P1689: [Usaco2005 Open] Muddy roads 泥泞的路
  8. Codeforces Gym 100342D Problem D. Dinner Problem Dp+高精度
  9. [转贴] C++ 判断主机是否处于联网状态下
  10. Linux Shell脚本中点号和source命令
  11. 使用 pm2 来守护 NoderCMS
  12. 简易的AJAX工具[转]
  13. PHP 两数组循环组合
  14. day-02
  15. JVM核心知识体系(转http://www.cnblogs.com/wxdlut/p/10670871.html)
  16. 数组的三种声明方式总结、多维数组的遍历、Arrays类的常用方法总结
  17. php5.5之后新特性整理
  18. java工程操作redis
  19. STL容器之vector
  20. PAT 1068 万绿丛中一点红(20)(测试点分析+思路分析)

热门文章

  1. Android中 Http请求
  2. [Tree]Binary Tree Inorder Traversal
  3. switch 与 whille相互套用
  4. LVS客户端启动脚本
  5. 关于Nexus 7的Usb host开发问题
  6. js调用打印机
  7. bind新发现
  8. hdu 5115 Dire Wolf(区间dp)
  9. 泛型、注解、log4j
  10. 列"xx"不在表Table中