a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
转化成:
[[1,4,7],[2,5,8],[3,6,9]]
原始版本:
此处需将dic中的key值手动完成,数据量较大时,该方法实用性不强
import itertools
condict = {"numeric":["one","two","three"],"ordinal":["first","second","third"],"numer":["1","2","3"]}
def f(x,y,z):
return [x,y,z]
v = itertools.imap(f, condict['numeric'], condict['ordinal'], condict['numer'])
na = []
for z in v:
na.append( z )
print na
升级版本:
import itertools
condict = {"numeric":["one","two","three"],"ordinal":["first","second","third"],"numer":["1","2","3"]}
def value_list():
arr = []
for (k,v) in condict.items():
arr.append(v)
# print arr #返回由value值组成的数组[['first', 'second', 'third'], ['1', '2', '3'], ['one', 'two', 'three']]
return arr
valuelist = value_list()
arr = []
for z in itertools.imap(lambda *args:args, *valuelist):
arr.append( z )
print arr #返回由各个数组对应位置组成的list
版本再升级,将上述方法合并到一个方法中
def value_list():
arr = []
for (k,v) in condict.items():
arr.append(v)
# print arr #返回由value值组成的数组[['first', 'second', 'third'], ['1', '2', '3'], ['one', 'two', 'three']]
arrlist = []
for z in itertools.imap(lambda *args:args,*arr):
arrlist.append( z )
print arrlist
value_list()
版本改造,利用map()函数
d = {"Title":["one","two","three"],"Description":["first","second","third"],"PicUrl":["1","2","3"], "Url":["u1","u2","u3"]}
def re_dic():
arr = []
for (k,v) in d.iteritems():
arr.append(v)
print map(None,*arr)
re_dic()
版本同级改造,强大的zip()函数
d = {"Title":["one","two","three"],"Description":["first","second","third"],"PicUrl":["1","2","3"], "Url":["u1","u2","u3"]}
def re_dic():
arr = []
for (k,v) in d.iteritems():
arr.append(v)
print zip(*arr)
re_dic()
以上例中的理想数据应该为:
[('u1', 'first', '1', 'one'), ('u2', 'second', '2', 'two'), ('u3', 'third', '3', 'three')]