zip,collections.defaultdict lambda,csv.DictReader用法简介,及subprocess.call注意事项(需要使用chmod将待执行的文件变为可执行)

定义:zip([iterable,...])

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

说明zip的一些使用方法:

a = [1,2,3]                                                                     
b = [4,5,6]
zipped = zip(a,b)
print zipped
b = zip(*zipped)
print b
a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] 
b = [ [row [col] for row in a] for col in range(len(a[0]))]
print b
c = zip(*a)
print c
d = map(list,zip(*a))
print d


运行结果:

[(1, 4), (2, 5), (3, 6)]

[(1, 2, 3), (4, 5, 6)]

[[1, 4, 7], [2, 5, 8], [3, 6, 9]]

[(1, 4, 7), (2, 5, 8), (3, 6, 9)]

[[1, 4, 7], [2, 5, 8], [3, 6, 9]]

zip的使用场景:

1、在一个循环中,同时访问多个可迭代的对象,比如列表等。不想写多个循环来遍历不同的可迭代对象。

a = [1,2,3]                                                                     
b = [3,4,5]	
for item in zip(a,b):
    print type(item)
    print item[0]
    print item[1]

2、使用两个可迭代的对象来构造字典。

a = [1,2,3]                                                                     
b = [3,4,5]
z = zip(a,b)
d = dict(z)
print z
print d


collections.defaultdict lambda:

defaultdict是字典类型,可以为defaultdict设置默认值可以通过lambda设置默认值

下面举几个例子:

from collections import *                                                      

x = defaultdict(lambda:0) //默认值是0

print x[0]

y =defaultdict(lambda:defaultdict(lambda:0))//默认值是一个字典,字典的默认值是0

print y[0]

z = defaultdict(lambda:[0,0,0])//默认值是一个列表,[0,0,0].

print z[0]


输出结果:

0

defaultdict(<function <lambda> at0x7f097797af50>, {})

[0, 0, 0]


csv.DictReader

处理csv文件的函数库:

DictWriterDictReader:读写带headercsv文件(类似表格)。

csv.reader,csv.writer:读写未带headercsv文件

import csv

FIELDS = ['Name', 'Sex', 'E-mail', 'Blog']

# DictWriter
csv_file = open('test.csv', 'wb')
writer = csv.DictWriter(csv_file, fieldnames=FIELDS) //写表头
# write header
print (zip(FIELDS,FIELDS))
print (dict(zip(FIELDS, FIELDS)))
writer.writerow(dict(zip(FIELDS, FIELDS)))
d = {}
d['Name'] = 'Qi'
d['Sex'] = 'Male'
d['E-mail'] = 'redice@163.com'
d['Blog'] = 'http://www.redicecn.com'

writer.writerow(d)
csv_file.close()
# For results, please see following picture

# DictReader
# A easier way for skipping the header
# Usually we need a extra flag variables
for d in csv.DictReader(open('test.csv', 'rb')): //读出来的格式是dict
    print d
# Output:
# {'Blog': 'http://www.redicecn.com', 'E-mail': 'redice@163.com', 'Name': 'Qi', 'Sex': 'Male'}


subprocess :开子进程执行脚本,谨记,一定要将脚本使用chmod变成可执行的。

	import subprocess
	cmd = './pre-b.py'
	subprocess.call(cmd, shell=True)

必须先把pre-b.py改为可执行的。

       -rwxr-xr-x 1 lujiandong lujiandong    3754 11月 19 10:24 pre-b.py*

如果pre-b.py是不可执行的,即:

-rw-rw-r-- 1 lujiandong lujiandong    3754 11月 19 10:24 pre-b.py

那么上面的方法就会报错:





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值