python简单示例
来源:互联网 发布:tcp和udp端口号区别 编辑:程序博客网 时间:2024/05/29 12:09
#coding:utf-8import randomimport re"""设计一个简单的摇骰子的游戏,摇三次骰子猜大小(3~10为小,大于等于10小于18为大)"""#shark the dice three timesdef get_list_of_dice(number =3,points =None): print('Start Rolling the dice:') if points ==None: points = [] while number >0: point = random.randrange(1,7) points.append(point) number = number -1 return points#get the result of the gamedef get_game_result(total): isSmall = 3<=total<10 isBig = 10<=total<18 if isSmall: print('The result is:Small') return 'Small' elif isBig : print('The result is:Big') return 'Big'#game startdef Start_game(): guess =input('please guess the result of Big or Small:') print(guess) list =get_list_of_dice() total = sum(list) result = get_game_result(total) print(result) if result == guess: print('Congratulation to you! You are Right!') else: print('Sorry ,You are wrong!')Start_game()"""正则表达式匹配电话号码和邮箱"""#re正则表达式匹配电话号码和邮箱# 正则匹配电话号码phone = "13893670000"p2 = re.compile('^0\d{2,3}\d{7,8}$|^1[358]\d{9}$|^147\d{8}')phonematch = p2.match(phone)if phonematch: print(phonematch.group())else: print("phone number is error!")# 正则匹配邮箱和电话号码emailorphone = "aaaaaaaaaa888@sina.cn"p3 = re.compile('^0\d{2,3}\d{7,8}$|^1[358]\d{9}$|^147\d{8}|[^\._-][\w\.-]+@(?:[A-Za-z0-9]+\.)+[A-Za-z]+')emailorphonematch = p3.match(emailorphone)if emailorphone: print(emailorphonematch.group())else: print("phone or email error...")"""对文件内的词汇进行词频统计
文件:
"""#词频统计path = '/Users/cykj/DeskTop/caoyajun/python/walden.txt'with open(path,'r') as text: words = text.read().split() print(words) for word in words: print('{}--{} times'.format(word,words.count(word)))"""存在问题:1。带标点符号的单词也被统计在内 2。Python对大小写敏感,统计区分了大小写"""#改进后import stringwith open(path,'r') as text: wods = [raw_word.strip(string.punctuation).lower() for raw_word in text.read().split()];#string.punctuation标点符号 words_index = set(wods) counts_dict = {index:wods.count(index) for index in words_index}for word in sorted(counts_dict.items(),key=lambda x: counts_dict[x],reverse=True): print('{} -- {} times'.format(word,counts_dict[word]))
result:
阅读全文
0 0
- python with 简单示例
- Python字典简单示例
- Python简单进度条示例
- python udp简单示例
- python TCP 简单示例
- python画散点图简单示例
- python简单示例
- python matplotlib简单示例
- Python socket 简单示例程序
- python发送邮件简单示例
- Python集合set简单示例
- Python中elif简单示例
- Python函数定义简单示例
- python 爬虫的简单示例
- Python的一些简单示例
- python之lambda简单示例
- Python 简单 Socket 编程示例
- 【Python】正则表达式-简单示例
- Android版本的"Wannacry"文件加密病毒样本分析(附带锁机)
- 如何在 build.gradle 中更整洁,更符合逻辑,更高效的书写代码
- Linux命令
- 反射
- If($i==true)和If(true==$i)区别
- python简单示例
- 自定义view实现一个游标效果(增强)
- JVM线上问题排查
- 关于视频播放
- 存储过程中处理用逗号拼接成的参数
- 深度学习在目标检测中的应用及其tensorflowAPI实践(一)
- 如何使用PDF转换器将PDF转换成图片
- linux nfs 命令
- java中的抽象类和接口