[英]How do I flatten nested lists of lists in python?
I have created a function to split paths into lists of directories in python like so: 我创建了一个函数,将路径拆分为python中的目录列表,如下所示:
splitAllPaths = lambda path: flatten([[splitAllPaths(start), end] if start else end for (start, end) in [os.path.split(path)]])
with this helper function: 使用此辅助功能:
#these only work one directory deep
def flatten(list_of_lists):
return list(itertools.chain.from_iterable(list_of_lists))
The output from this function looks like so: 该函数的输出如下所示:
> splitAllPaths('./dirname/dirname2/foo.bar')
[[[['.'], 'dirname'], 'dirname2'], 'foo.bar']
now I want this as a flat list. 现在,我希望将此作为一个清单。 my attempts are as follows (with the output):
我的尝试如下(带有输出):
> flatten(splitAllPaths('./diname/dirname2/foo.bar'))
['.', 'd', 'i', 'r', 'n', 'a', 'm', 'e', 'd', 'i', 'r', 'n', 'a', 'm', 'e', '2', 'f', 'o', 'o', '.', 'b', 'a', 'r']
and 和
> reduce(list.__add__, (list(mi) for mi in splitAllPaths('./dirname/dirname2/foo.bar')))
me2/foo.bar')))
[[['.'], 'dirname'], 'dirname2', 'f', 'o', 'o', '.', 'b', 'a', 'r']
How do I unfold this list correctly (I would also welcome any suggestions for how to improve my splitAllPaths
function)? 如何正确显示此列表(我也欢迎任何有关改进我的
splitAllPaths
函数的建议)?
This a less general answer, but it solves your original problem -- although its elegance is debatable. 这个答案不太笼统,但可以解决您的原始问题-尽管其优雅之处值得商bat。
The main idea is the fact that generating a list with the reversed (as in ['file', 'user', 'home', '/']
order is quite easy, so you can just create that and reverse it in the end. So it boils down to: 主要思想是这样的事实:生成具有颠倒顺序 (如
['file', 'user', 'home', '/']
顺序)的列表非常容易,因此您可以创建它并最后对其进行颠倒因此归结为:
def split_paths(path):
def split_paths_reverse(path):
head, tail = os.path.split(path)
while head and tail:
yield tail
head, tail = os.path.split(head)
yield head
return reversed(tuple(split_paths_reverse(path)))
Example: 例:
test = '/home/user/file.txt'
print(list(split_paths(test)))
['/', 'home', 'user', 'file.txt']
You could also avoid the explicit reversing part by putting each element in a stack and then removing them, but that's up to you. 您还可以通过将每个元素放入堆栈中然后将其删除来避免显式反转部分,但这取决于您。
Sortherst way that comes in mind would be: 想到的排序方式是:
listoflists = [[[['.'], 'dirname'], 'dirname2'], 'foo.bar']
str(listoflists).translate(None,"[]'").split(',')
I solved this by writing a (non-general) foldr. 我通过编写一个(非通用的)文件夹解决了这个问题。 I think better, more practical solutions are provided by @L3viathan in the comments.
我认为@ L3viathan在评论中提供了更好,更实用的解决方案。
attempt = lambda list: attempt(list[0] + list[1:]) if len(list[0]) > 1 else list[0] + list[1:]
Output 产量
> attempt([[[['.'], 'dirname'], 'dirname2'], 'foo.bar'])
['.', 'dirname', 'dirname2', 'foo.bar']
I've also now written it in terms of a general foldr1
我现在也用通用文件
foldr1
写它
> foldr1 = lambda func, list: foldr1(func, func(list[0], list[1:])) if len(list[0]) > 1 else func(list[0], list[1:])
> foldr1(list.__add__, [[[['.'], 'dirname'], 'dirname2'], 'foo.bar'])
['.', 'dirname', 'dirname2', 'foo.bar']
NOTE: Could someone more familiar than me confirm that this is a foldr and not a foldl (I often get them confused). 注意:能比我更熟悉的人确认这是一个文件夹而不是一个文件夹吗(我经常使他们感到困惑)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.