奥特蟹蟹 发表于 2014-10-30 13:16

去掉数组中嵌套数组括号的方法

今天写数据库接口程序的时候,需要用到fetchall函数,但是该函数返回的数组格式是[(a),]格式的,而我要用symmetric_difference函数来获得对称差,于是需要去掉fetchall函数返回数组中嵌套数组的括号。谷歌了一下如何实现,结果找到3种实现方式,现跟大家分享如下:


方法1、
>>>import itertools
>>> list2d = [,, , ]
>>> merged = list(itertools.chain.from_iterable(list2d))

方法2、>>> sum(l, [])                        """Note that only works on lists of lists. For lists of lists of lists, you'll need another solution."""

方法3、
>>> import operator
>>> l = [,, , ]
>>> reduce(operator.add, l)

以上3种方法运行的结果都是:

HH106 发表于 2014-10-30 14:01

本帖最后由 HH106 于 2014-10-30 14:01 编辑

感谢楼主分享,赞一个
页: [1]
查看完整版本: 去掉数组中嵌套数组括号的方法