python兼并列表的要领以下:
要领一
最原始,最笨的要领,分别从两个列表中掏出一切的元素,再放入新列表中就OK了。示例代码以下:
list1 = [1,2,3] list2 = [4,5,6] list_new = [] for item in list1: list_new.append(item) for item in list2: list_new.append(item) print(list_new)
要领二
这里要用到python中的一个内置函数zip(),它的作用从名字就能够看出来,就是将几个原本无关的内容打包到一同。看代码:
a = [1,2,3] b = [4,5,6] c = zip(a,b) //c = [(1,4),(2,5),(3,6)] list_new = [row[i] for i in range(len(0)) for row in c]
此处为python2语法
要领三
直接运用+兼并
a = [1,2,3] b = [4,5,6] c = a + b
要领四
运用append要领
a = [1,2,3] b = [4,5,6] a.append(b)
注:append()要领是将b列表作为一个团体添加到a列表中,输出效果为
[1, 2, 3, [4, 5, 6]]
以上就是python中怎样兼并两个列表的细致内容,更多请关注ki4网别的相干文章!