Python unfolds and tiles two layers of lists into one layer. Five implementations of of
- 2021-10-25 07:17:46
- OfStack
These days, I have been discussing with my colleagues how to write elegantly with Python, so that the list in the list can be expanded and become a flat list.
For example
# Expected input
input = [[('A', 1), ('B', 2)], [('C', 3), ('D', 4)]]
# Expected output
output = [('A', 1), ('B', 2), ('C', 3), ('D', 4)]
map function merging
>>> new = []; map(new.extend, input); new
[None, None]
[('A', 1), ('B', 2), ('C', 3), ('D', 4)]
This method looks OK, but it has a fatal disadvantage, that is, the map function returns a value, and this return value is useless. In addition, it is necessary to declare a variable in advance, which is not concise and elegant enough from the simplicity of the code.
sum function merging
>>> sum(input, [])
[('A', 1), ('B', 2), ('C', 3), ('D', 4)]
This looks simple, but there are performance pitfalls like string accumulation. There is a performance comparison in the back.
reduce function
>>> reduce(list.__add__, input)
[('A', 1), ('B', 2), ('C', 3), ('D', 4)]
Do sequence accumulation operation. There are also cumulative performance traps.
List derivation
>>> [item for sublist in input for item in sublist]
[('A', 1), ('B', 2), ('C', 3), ('D', 4)]
The list derivation looks a little long, and it takes for to loop twice. It takes a little effort to understand it into one line, which is not so intuitive.
itertools class library
>>> list(itertools.chain(*input))
[('A', 1), ('B', 2), ('C', 3), ('D', 4)]
Through the third class library class implementation, compared with other several implementations, it looks more elegant. The final performance discovery is actually very high.
Great performance contrast
python -mtimeit -s'l=[[1,2,3],[4,5,6], [7], [8,9]]*99' 'reduce(list.__add__,l)'
1000 loops, best of 3: 547 usec per loop
python -mtimeit -s'l=[[1,2,3],[4,5,6], [7], [8,9]]*99' 'sum(l, [])'
1000 loops, best of 3: 509 usec per loop
python -mtimeit -s'l=[[1,2,3],[4,5,6], [7], [8,9]]*99' '[item for sublist in l for item in sublist]'
10000 loops, best of 3: 52.8 usec per loop
python -mtimeit -s'l=[[1,2,3],[4,5,6], [7], [8,9]]*99; import itertools;' 'list(itertools.chain(*l))'
10000 loops, best of 3: 35.9 usec per loop
python -mtimeit -s'l=[[1,2,3],[4,5,6], [7], [8,9]]*99' 'new = []; map(new.extend, l); new'
10000 loops, best of 3: 34.1 usec per loop
Welcome to discuss elegant implementation and performance optimization.
Addition: python Tiles (including nested) dict
Without saying much, go directly to the code:
def prefix_dict(di_, prefix_s=''):
"""
Put each of the dictionaries key They all come with prefixes prefix_s
:param di_:
:param prefix_s:
:return:
"""
return {prefix_s + k: v for k, v in di_.items()}
def spear_dict(di_, con_s='.'):
"""
Unfold dict( If the lower level is still dict) Which requires recursion and expands until the lower data type is not a dictionary
Possible Usefulness: It may be useful to format the data of the document class to look more relational
:param di_: Input dictionary
:param con_s: Connection symbols between levels
:return: The depth is not greater than 1 Other nested data types remain the same
"""
ret_di = {}
for k, v in di_.items():
if type(v) is dict:
v = spear_dict(v)
# There may be something here that doesn't write here 1 A better way to write layers
# for k_, v_ in v.items():
# ret_di.update({con_s.join([k, k_]): v_})
ret_di.update(prefix_dict(v, prefix_s=k + con_s))
else:
ret_di.update({k: v})
return ret_di
>>> di_
{'title': ' San Tin Commercial Street ', 'reliability': 7, 'addressComponents': {'streetNumber': '', 'city': ' Shenzhen ', 'street': '', 'province': ' Guangdong Province ', 'district': ' Longhua district '}, 'location': {'lng': 114.09127044677734, 'lat': 22.700519561767578}, 'adInfo': {'adcode': '440309'}, 'level': 11, 'more_deep': {'loca': {'lng': 114.09127044677734, 'lat': 22.700519561767578}}}
>>> spear_dict(di_)
{'title': ' San Tin Commercial Street ', 'reliability': 7, 'addressComponents.streetNumber': '', 'addressComponents.city': ' Shenzhen ', 'addressComponents.street': '', 'addressComponents.province': ' Guangdong Province ', 'addressComponents.district': ' Longhua district ', 'location.lng': 114.09127044677734, 'location.lat': 22.700519561767578, 'adInfo.adcode': '440309', 'level': 11, 'more_deep.loca.lng': 114.09127044677734, 'more_deep.loca.lat': 22.700519561767578}
spear_dict(di_, '_')
{'title': ' San Tin Commercial Street ', 'reliability': 7, 'addressComponents_streetNumber': '', 'addressComponents_city': ' Shenzhen ', 'addressComponents_street': '', 'addressComponents_province': ' Guangdong Province ', 'addressComponents_district': ' Longhua district ', 'location_lng': 114.09127044677734, 'location_lat': 22.700519561767578, 'adInfo_adcode': '440309', 'level': 11, 'more_deep_loca.lng': 114.09127044677734, 'more_deep_loca.lat': 22.700519561767578}