python学习
文章平均质量分 70
spyao
这个作者很懒,什么都没留下…
展开
-
python-变量命名规范
块名、包名: 小写字母,单词之间用_分割 ad_stats.py 类名: (每个)单词首字母都要大写 AdStats ConfigUtil 全局变量名: 大写字母,单词之间用_分割 NUMBER COLOR_WRITE 普通变量: 小写字母,单词之间用_分割 this_is_a_var 常量 常量名所有字母大写,由下划线连转载 2015-12-09 20:12:02 · 940 阅读 · 0 评论 -
python--python3爬虫之模拟登录知乎
代码在python3环境下测试通过:from bs4 import BeautifulSoupimport requestsurl = 'http://www.zhihu.com'login_url = url+'/login/email'captcha_url = 'http://www.zhihu.com/captcha.gif'headers={ 'Accep原创 2015-12-21 20:30:53 · 5612 阅读 · 1 评论 -
python--字典排序
prices = { 'ACME': 45.23, 'AAPL': 612.78, 'IBM': 205.55, 'HPQ': 37.20, 'FB': 10.75}对于这样一个字典,如何根据价格来对字典进行排序呢?首先你就会问,python中的字典明明是无序的,何谈排序之说?别急,办法总是有的。对于这样一个字典的排序,直接使用sorted函数肯定原创 2015-12-05 11:27:55 · 760 阅读 · 0 评论 -
python--10行代码搞定词频统计
问题描述:现在有两篇英文电子书(含中文行),统计他们各自的单词出现次数并进行加和,结果以字典形式呈现:{'the': 2154, 'and': 1394, 'to': 1080, 'of': 871, 'a': 861, 'his': 639, 'The': 637, 'in': 515, 'he': 461, 'with': 310, 'that': 308, 'you': 295, 'fo原创 2015-12-05 15:44:36 · 54249 阅读 · 3 评论 -
python--PIL初探
from PIL import *image=Image.open('10.jpg')#box = (20, 0,70,55)#region = image.crop(box)#region = region.rotate(180)#image.paste(region,box)#width,height = image.size#image2=image.resize((width原创 2015-12-22 23:12:18 · 782 阅读 · 0 评论 -
python--删除队列重复元素
如何删除队列重复元素呢?简单地说,我们有两种实现的方法:1.借助set函数先将列表转化为集合:a=[1,3,1,2,4,2,5,6]print(list(set(a)))这种方法有两个局限:第一,转化后的list不再保持原有的顺序(因为set和dict一样是无序的);第二,如果列表中存在可变元素(list,dict,etc),那么也不能借助set来实现:a=[1,3,1,2,[4原创 2015-12-05 14:49:36 · 5915 阅读 · 1 评论 -
python--requests下载图片
转载自http://stackoverflow.com/questions/13137817/how-to-download-image-using-requestsYou can either use the response.raw file object, or iterate over the response.To use the response.raw file-转载 2015-12-22 10:48:04 · 5955 阅读 · 0 评论 -
python--格式化打印
1.字符串方法(ljust,rjust,center,format)>>> s='格式化打印'>>> print(s.ljust(40,'-'))'格式化打印-----------------------------------'>>> print(s.rjust(40,'-'))-----------------------------------格式化打印>>> print(s.原创 2015-12-14 08:54:36 · 1123 阅读 · 0 评论 -
python--回调函数
学习时遇到这样段代码:import retext = 'UPPER PYTHON, lower python, Mixed Python'def replace(m,word): text = m.group() if text.isupper(): return word.upper() elif text.islower():原创 2015-12-13 19:49:20 · 1543 阅读 · 0 评论 -
python--格式化打印杨辉三角(format)
打印目标:>>> runfile('E:/桌面/代码池/untitled0.py', wdir='E:/桌面/代码池')num:11 1 1 1原创 2015-12-06 17:08:15 · 16019 阅读 · 0 评论 -
python--更干净的词频统计
上篇文章我们介绍了利用Counter模块轻松搞定词频统计的方法,因为重点是介绍模块的使用,代码显得比较粗糙。import re,collectionsdef get_words(file): with open (file) as f: words_box=[] for line in f:原创 2015-12-05 17:07:00 · 15397 阅读 · 3 评论 -
python--字典初始化
如果说deque是加强版的list的话,那加强版的字典又是什么的呢?没错,就是今天学习的defaultdict,它与deque一样,都是在collections库中的模块。先来看这样一个问题:s = [('Tom', 5), ('Jone', 2), ('Susan', 4), ('Tom', 4), ('Tom', 1)]对于这样一个元组列表,如果我们想要将它转化为字典,该如何操作呢?我们原创 2015-12-04 16:29:45 · 38841 阅读 · 0 评论