python tips(2)
2013-05-11 15:06
309 查看
1. python Popen的具体解释是:打开一个命令或从管道返回值。
从python2.4开始,就可以用subprocess这个模块来产生子进程,并连接到子进程的标准输入/输出/错误中去,还可以得到子进程的返回值,subprocess意在替代其它几个老的模块或函数。如,os.system, os.spawn*, os.popen*, popen2.*, commands.*
下面是使用subprocess的例子。
2. python新建进程用os.fork函数,但它只在POSIX系统上可用,在Windows版的python中,os模块没有定义os.fork函数,相反,Windows程序员用多线程编程技术(multiprocessing)来完成并发任务。
关于python fork:
1)创建管道 2)创建子进程
子进程:
1)需要关闭管道读端 2)开始执行 3)向写端写结果 4)进程死亡
父进程:
1)关闭管道写端 2)从读端读取数据直到子进程死亡或者关闭 3)调用waitpid方法确保子进程已经被撤销(在FreeBSD中不这么做子进程永远不会死亡) 4)进程输出
3. map, reduce, filter的使用例子。
输出结果为:
4. python 冒泡排序的两种方法。
方法一:
方法二:
结果如下:
5. 巧用python输出。
结果如下:
6. python的函数可以返回多个值,例子如下。
结果如下:
7. python遍历字典元素时字典元素的顺序通常没有定义,换句话说,迭代的时候,字典中的键和值都保证被处理,但是处理顺序不确定,如果顺序很重要的话,可以将键值保存在单独的列表中,例如迭代前进行排序。
8. cmd命令"netsh wlan export profile"可以让用户将一个在Windows图形用户界面中创建的无线配置文件导出到一个XML文件中,如此,便可将其导入到其它计算机中或进行备份。
附:推荐几本学习Python的书籍,也供备查。
1. Learning Python, O'Reilly, Introduction
2. Pratical Python, APress, 部分实用程序
3. Python Standard Library, Fredrik Lundh, 模块实例
4. Python Cookbook, (第2版)中文版, O'Reilly, 人邮, 进阶
从python2.4开始,就可以用subprocess这个模块来产生子进程,并连接到子进程的标准输入/输出/错误中去,还可以得到子进程的返回值,subprocess意在替代其它几个老的模块或函数。如,os.system, os.spawn*, os.popen*, popen2.*, commands.*
下面是使用subprocess的例子。
import win32com.client from subprocess import Popen import time shell = win32com.client.Dispatch("WScript.Shell") shell.Run("notepad") time.sleep(5) Popen("taskkill /f /im notepad.exe") Popen("calc") time.sleep(5) Popen("taskkill /f /im calc.exe")
2. python新建进程用os.fork函数,但它只在POSIX系统上可用,在Windows版的python中,os模块没有定义os.fork函数,相反,Windows程序员用多线程编程技术(multiprocessing)来完成并发任务。
关于python fork:
1)创建管道 2)创建子进程
子进程:
1)需要关闭管道读端 2)开始执行 3)向写端写结果 4)进程死亡
父进程:
1)关闭管道写端 2)从读端读取数据直到子进程死亡或者关闭 3)调用waitpid方法确保子进程已经被撤销(在FreeBSD中不这么做子进程永远不会死亡) 4)进程输出
3. map, reduce, filter的使用例子。
def map_func(lis): return lis + 1 def reduce_func(li, lis): return li + lis def filter_func(lis): if lis % 2 == 0: return True else: return False lis = [1, 2, 3, 4, 5] map_I = map(map_func, lis) reduce_I = reduce(reduce_func, lis) filter_I = filter(filter_func, lis) print map_I print reduce_I print filter_I
输出结果为:
>>> [2, 3, 4, 5, 6] 15 [2, 4] >>>
4. python 冒泡排序的两种方法。
方法一:
array = [1, 2, 5, 3, 6, 8, 4] for i in range(len(array) - 1, 1, -1): #print i for j in range(0, i): #print j if array[j] > array[j + 1]: array[j], array[j + 1] = array[j + 1], array[j] print array
方法二:
array1 = [1, 2, 5, 3, 6, 8, 4] array1.sort(cmp = None, key = None, reverse = False) print array1
结果如下:
>>> [1, 2, 3, 4, 5, 6, 8] [1, 2, 3, 4, 5, 6, 8] >>>
5. 巧用python输出。
array = [1, 2, 5, 3, 6, 8, 4] # (0, 1, 2, 3, 4, 5, 6) # (-7,-6,-5,-4,-3,-2,-1) print array[::2] print array[2::] print array[::-1] print array[0:] print array[:-1] print array[3:-3]
结果如下:
>>> [1, 5, 6, 4] [5, 3, 6, 8, 4] [4, 8, 6, 3, 5, 2, 1] [1, 2, 5, 3, 6, 8, 4] [1, 2, 5, 3, 6, 8] [3] >>>
6. python的函数可以返回多个值,例子如下。
def test(num = 0): return(num, num + 1) def hello(num = 0): if num == 0: pass else: return num + 2 num1, num2 = test(1) num4 = hello(1) num3 = hello(0) print num1, num2 print num3, num4
结果如下:
>>> 1 2 None 3 >>>
7. python遍历字典元素时字典元素的顺序通常没有定义,换句话说,迭代的时候,字典中的键和值都保证被处理,但是处理顺序不确定,如果顺序很重要的话,可以将键值保存在单独的列表中,例如迭代前进行排序。
8. cmd命令"netsh wlan export profile"可以让用户将一个在Windows图形用户界面中创建的无线配置文件导出到一个XML文件中,如此,便可将其导入到其它计算机中或进行备份。
附:推荐几本学习Python的书籍,也供备查。
1. Learning Python, O'Reilly, Introduction
2. Pratical Python, APress, 部分实用程序
3. Python Standard Library, Fredrik Lundh, 模块实例
4. Python Cookbook, (第2版)中文版, O'Reilly, 人邮, 进阶
相关文章推荐
- python tips;matplotlib 显示中文
- Python 数据处理的 Some tips --- 索引
- Python tips: 超时装饰器, @timeout decorator
- Python性能鸡汤,非常有效的提高性能的tips
- 基础 python(三)关于string的几个tips
- some tips about python default value....
- python多进程共享变量Value使用tips
- Python tips: 什么是*args和**kwargs?
- 10 Python Optimization Tips and Issues
- Python Tips阅读摘要
- Python Tips
- Python学习中的tips(一)
- python tips
- python tips - 注意函数参数的默认值
- python技巧31[pythonTips1]
- Python tips: 什么是*args和**kwargs?
- 【20180221】python-tips-正则匹配
- python小TIPS
- come tips about python Five
- python tips(持续更新)