python list comprehensions; compressing a list of

2019-01-13 05:26发布

guys. I'm trying to find the most elegant solution to a problem and wondered if python has anything built-in for what I'm trying to do.

What I'm doing is this. I have a list, A, and I have a function f which takes an item and returns a list. I can use a list comprehension to convert everything in A like so;

[f(a) for a in A]

But this return a list of lists;

[a1,a2,a3] => [[b11,b12],[b21,b22],[b31,b32]]

What I really want is to get the flattened list;

[b11,b12,b21,b22,b31,b32]

Now, other languages have it; it's traditionally called flatmap in functional programming languages, and .Net calls it SelectMany. Does python have anything similar? Is there a neat way to map a function over a list and flatten the result?

The actual problem I'm trying to solve is this; starting with a list of directories, find all the subdirectories. so;

import os
dirs = ["c:\\usr", "c:\\temp"]
subs = [os.listdir(d) for d in dirs]
print subs

currentliy gives me a list-of-lists, but I really want a list.

13条回答
Fickle 薄情
2楼-- · 2019-01-13 05:52

You can have nested iterations in a single list comprehension:

[filename for path in dirs for filename in os.listdir(path)]
查看更多
小情绪 Triste *
3楼-- · 2019-01-13 05:53

You can concatenate lists using the normal addition operator:

>>> [1, 2] + [3, 4]
[1, 2, 3, 4]

The built-in function sum will add the numbers in a sequence and can optionally start from a specific value:

>>> sum(xrange(10), 100)
145

Combine the above to flatten a list of lists:

>>> sum([[1, 2], [3, 4]], [])
[1, 2, 3, 4]

You can now define your flatmap:

>>> def flatmap(f, seq):
...   return sum([f(s) for s in seq], [])
... 
>>> flatmap(range, [1,2,3])
[0, 0, 1, 0, 1, 2]

Edit: I just saw the critique in the comments for another answer and I guess it is correct that Python will needlessly build and garbage collect lots of smaller lists with this solution. So the best thing that can be said about it is that it is very simple and concise if you're used to functional programming :-)

查看更多
走好不送
4楼-- · 2019-01-13 06:00
If listA=[list1,list2,list3]
flattened_list=reduce(lambda x,y:x+y,listA)

This will do.

查看更多
叼着烟拽天下
5楼-- · 2019-01-13 06:03

You can find a good answer in itertools' recipes:

def flatten(listOfLists):
    return list(chain.from_iterable(listOfLists))

(Note: requires Python 2.6+)

查看更多
神经病院院长
6楼-- · 2019-01-13 06:04

You could just do the straightforward:

subs = []
for d in dirs:
    subs.extend(os.listdir(d))
查看更多
Rolldiameter
7楼-- · 2019-01-13 06:05
import itertools
x=[['b11','b12'],['b21','b22'],['b31']]
y=list(itertools.chain(*x))
print y

itertools will work from python2.3 and greater

查看更多
登录 后发表回答