python list comprehensions; compressing a list of lists?

后端 未结 13 1990
名媛妹妹
名媛妹妹 2020-11-30 01:49

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

13条回答
  •  我在风中等你
    2020-11-30 02:18

    You could try itertools.chain(), like this:

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

    itertools.chain() returns an iterator, hence the passing to list().

提交回复
热议问题