随笔分类 -  python

python生成器
摘要:def func_list_empty(l = []): l.append(2) return l def func_list_none(l = None): if l == None: l = [] l.append(2) return l def test_list_empty(l = []): func_l... 阅读全文

posted @ 2016-08-03 15:59 与非朋仔 阅读(169) 评论(0) 推荐(0)

IP子网掩码格式转换
摘要:def exchange_maskint(mask_int): bin_arr = ['0' for i in range(32)] for i in range(mask_int): bin_arr[i] = '1' tmpmask = [''.join(bin_arr[i * 8:i * 8 + 阅读全文

posted @ 2016-08-02 14:30 与非朋仔 阅读(816) 评论(0) 推荐(0)

Python中exec的使用
摘要:>>>globals = {'x':7, .....: 'y':10, .....: 'names':['aa','bb','cc'] .....: } >>>locals = {} >>>a = eval("3*x+4*y",globals,locals) >>>print a 61 >>>exe 阅读全文

posted @ 2016-05-24 19:45 与非朋仔 阅读(1543) 评论(0) 推荐(0)

partial 函数
摘要:函数的partial应用 函数在执行时,要带上所有必要的参数进行调用。但是,有时参数可以在函数被调用之前提前获知。这种情况下,一个函数有一个或多个参数预先就能用上,以便函数能用更少的参数进行调用。 例如: In [9]: from functools import partial In [10]: 阅读全文

posted @ 2016-05-22 15:26 与非朋仔 阅读(276) 评论(0) 推荐(0)

map函数
摘要:list是一个函数,将参数强制转换成列表list((1,4,7)) 对元组(1,4,7)使用list函数就返回列表[1,4,7]map(list,zip(*a))表示对zip(*a)的每一个单位都执行list函数而且这在python 2.6.6中执行正常,执行效果如下>>> a=[[1,2,3],[ 阅读全文

posted @ 2016-05-21 21:57 与非朋仔 阅读(171) 评论(0) 推荐(0)

python命令行上下 退格,左右键不能用
摘要:easy_install readline 阅读全文

posted @ 2016-05-21 20:57 与非朋仔 阅读(998) 评论(0) 推荐(0)

python字典操作
摘要:输出结果: {"sub_dic": {"sub_str": "this is sub str", "sub_list": [1, 2, 3]}, "end": "end", "list": [1, 2, "a", "b"], "str": "this is a string"} 将两列list组成一 阅读全文

posted @ 2016-05-19 17:09 与非朋仔 阅读(128) 评论(0) 推荐(0)

isinstance使用方法
摘要:#!/usr/bin/python2.7 def displayNumType(num): print num, 'is', if isinstance(num,(int, long, float, complex)): print 'a num of type:', type(num).__nam 阅读全文

posted @ 2016-04-27 23:44 与非朋仔 阅读(464) 评论(0) 推荐(0)

python顶级执行代码
摘要:只有主程序中由大量顶级执行代码(即没有被缩进的代码行),所有其他被导入的模块只应该又很少的顶级执行代码。 如果模块是被导入,__name__就是模块名。 如果模块是被直接执行,__name__就是__main__。 阅读全文

posted @ 2016-04-11 00:16 与非朋仔 阅读(459) 评论(0) 推荐(0)

python with语句详解
摘要:While this works well, it is unnecessarily verbose. This is where with is useful. The good thing about with apart from the better syntax is that it is 阅读全文

posted @ 2016-04-10 20:39 与非朋仔 阅读(449) 评论(0) 推荐(0)

树莓派的python脚本模块布局
摘要:#!/usr/bin/python2.7 "this is my raspberrypi python layout" import heapq debug = True class PriorityQueue: def __init__(self): self._queue = [] self._index = 0 def push(... 阅读全文

posted @ 2016-04-10 15:48 与非朋仔

核心编程第四章python内建函数
摘要:python使用is和is not来测试两个变量是否属于同一个对象。 a is b 等价于 id(a) == id(b) a与b的值都是1,由于被缓存,所以a,b指向同一整形对象。只有小整数才会被缓存。 repr()和··相同的功能,返回一个给机器看的官方对象的字符串表示,绝大多数情况下可以通过求值 阅读全文

posted @ 2016-04-10 00:57 与非朋仔

第一个python实例程序
摘要:#!/usr/bin/python2.7 import os ls = os.linesep fname = raw_input("fname:"); while True: if os.path.exists(fname): print "Error:'%s' already exists" % fname ... 阅读全文

posted @ 2016-04-06 00:02 与非朋仔 阅读(240) 评论(0) 推荐(0)

type()
摘要:pi@raspberrypi ~ $ python Python 2.7.3 (default, Mar 18 2014, 05:13:23) [GCC 4.6.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> type('') >>> s = 'xyz' >>> t... 阅读全文

posted @ 2016-04-05 23:25 与非朋仔 阅读(283) 评论(0) 推荐(0)

pi
摘要:pi@raspberrypi ~ $ python Python 2.7.3 (default, Mar 18 2014, 05:13:23) [GCC 4.6.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> names = ('xupeng','yanfeng','... 阅读全文

posted @ 2016-04-05 22:59 与非朋仔 阅读(188) 评论(0) 推荐(0)

python
摘要:None是一个特殊的常量。 None和False不同。 None不是0。 None不是空字符串。 None和任何其他的数据类型比较永远返回False。 None有自己的数据类型NoneType。 你可以将None复制给任何变量,但是你不能创建其他NoneType对象。 阅读全文

posted @ 2016-04-05 22:47 与非朋仔 阅读(118) 评论(0) 推荐(0)

if __name__ == '__main__'在python中的应用
摘要:当你打开一个.py文件时,经常会在代码的最下面看到if __name__ == '__main__':,现在就来介 绍一下它的作用. 模块是对象,并且所有的模块都有一个内置属性 __name__。一个模块的 __name__ 的值取决于您如何应用模块。如果 import 一个模块,那么模块__nam 阅读全文

posted @ 2016-04-05 17:04 与非朋仔 阅读(168) 评论(0) 推荐(0)

导航