- 论坛徽章:
- 0
|
今天写数据库接口程序的时候,需要用到fetchall函数,但是该函数返回的数组格式是[(a),]格式的,而我要用symmetric_difference函数来获得对称差,于是需要去掉fetchall函数返回数组中嵌套数组的括号。谷歌了一下如何实现,结果找到3种实现方式,现跟大家分享如下:
方法1、
>>>import itertools
>>> list2d = [[1,2,3],[4,5,6], [7], [8,9]]
>>> 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 = [[1,2,3],[4,5,6], [7], [8,9]]
>>> reduce(operator.add, l)
以上3种方法运行的结果都是:[1, 2, 3, 4, 5, 6, 7, 8, 9] |
|