www.4585.net > python 两个列表合并

python 两个列表合并

~$ pythonPython 2.7.3 (default, Mar 14 2014, 11:57:14) [GCC 4.7.2] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> a = [1, 2, 5, 8 ,12]>>> b = [25, 13, 6, 9]>>> a + b[1, 2, 5, 8, 12, 25, 13, 6...

给个代码示例吧: a=[1,2,3] b=[4,5,6] c=a[:] c=c+b #此时列表c的内容是a与b合并后的内容 如果是原地合并,即把a与b的内容合并到a,则代码如下: a=[1,2,3] b=[4,5,6] a.extend(b) #此时列表a的内容是a与b合并后的内容

除了直接相加(生成新的list),还有两种方法(修改其中一个list): 用list的extend方法,L1.extend(L2),该方法将参数L2的全部元素添加到L1的尾部,例如: >>> L1 = [1, 2, 3, 4, 5]>>> L2 = [20, 30, 40]>>> L1.extend(L2)>>> L1[1, 2, 3, 4, 5,...

用extend(): Python 3.2.3 (default, Apr 11 2012, 07:15:24) [MSC v.1500 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>> A = [1, 2, 3] >>> B = [4, 5, 6] >>> A1 = A[:] >>> A.append( B ...

比如:l1=[1,2,3] l2=['baidu','google','bing'] py3.x中 dict(map(lambda x,y:[x,y], l1,l2)) {1:'baidu', 2:'google', 3:'bing'} py2.x中 dict(zip(l1,l2))

1.简介:Python 中 list 的合并操作 2.所需工具/原料: python2.7 3.方法: l1=['L','O','L']l2=['lu','a','lu']# 将两个list合二为一l1+l2 (或 l1.extend(l2))#运行结果:['L', 'O', 'L', 'lu', 'a', 'lu']# 将两个list捆绑zip(l1,l2)#运行结果...

Python编程将多个文件合并,代码如下: #例子:合并a.txt、b.txt、c.txt合并成d.txt文件 #文件列表,遍于读取 flist = ['a.txt','b.txt','c.txt'] #要写入的文件 ofile = open('d.txt', 'w') #遍历读取所有文件,并写入到输出文件 for fr in fli...

~$ python Python 2.7.3 (default, Mar 14 2014, 11:57:14) [GCC 4.7.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> a = [1, 2, 5, 8 ,12] >>> b = [25, 13, 6, 9] >>> a + b [1, 2, 5, 8, 12, 25,...

用 zip 函数进行组合,然后把返回的每个元素转 list c=[list(i) for i in zip(a, b)]

网站地图

All rights reserved Powered by www.4585.net

copyright ©right 2010-2021。
www.4585.net内容来自网络,如有侵犯请联系客服。zhit325@qq.com