The list in Python is known to be extend, and is designed to combine two lists into one. For example [1,2,3].extend ([4,5,6]) =[1,2,3,4,5,6]
If there is a list of list, I want to reduce them into a list, how to do? People who know a bit of functional programming will think of reduce, but direct reduce (lambda x,y:x.extend (y), lists) is not possible because the original implementation in the Python list class does not allow chained extend.
My workaround is to inherit the list class to add chained extend operations.
1 class listwithlinkextend (list): 2 def Extend (self, value): 3 Super (Listwithlinkextend, self). Extend (value)4 return Self
This makes it easy to reduce the list of lists by chaining extend.
1 x = [[], [4,5,6], [7,8,9]]2 list (reduce (lambda A, B: Listwithlinkextend (a). Extend (Listwithlinkextend (b)), X))
[Python] tips for solving python chained extend