呼唤远方
对于您的小示例,列表理解比数组方法更快,即使将数组创建从计时循环中取出:In [204]: list_of_lists = [["a","b","c"], ["d","e","f"], ["g","h","i"]] ...: flattened_list = [i for j in list_of_lists for i in j] In [205]: timeit [i for j in list_of_lists for i in j] 757 ns ± 17.3 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)In [206]: np.ravel(list_of_lists) Out[206]: array(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'], dtype='<U1')In [207]: timeit np.ravel(list_of_lists) 8.05 µs ± 12.4 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)In [208]: %%timeit x = np.array(list_of_lists) ...: np.ravel(x) 2.33 µs ± 22.8 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)有了一个更大的例子,我希望 [208] 会变得更好。如果子列表大小不同,则数组不是 2d,flatten 什么也不做:In [209]: list_of_lists = [["a","b","c",23], ["d",None,"f"], ["g","h","i"]] ...: flattened_list = [i for j in list_of_lists for i in j] In [210]: flattened_list Out[210]: ['a', 'b', 'c', 23, 'd', None, 'f', 'g', 'h', 'i']In [211]: np.array(list_of_lists) Out[211]: array([list(['a', 'b', 'c', 23]), list(['d', None, 'f']), list(['g', 'h', 'i'])], dtype=object)增长列表更有效:In [217]: alist = [] In [218]: for row in list_of_lists: ...: alist.append(row) ...: In [219]: alist Out[219]: [['a', 'b', 23], ['d', None, 'f'], ['g', 'h', 'i']]In [220]: np.array(alist) Out[220]: array([['a', 'b', 23], ['d', None, 'f'], ['g', 'h', 'i']], dtype=object)我们强烈反对迭代连接。首先收集列表中的子列表或数组。