forked from chrisconlan/fast-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flatten_lists.py
129 lines (103 loc) · 3.41 KB
/
flatten_lists.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
"""
Given a list of lists of strings, flatten into a single
list of strings
"""
import random
import string
import numpy as np
import pandas as pd
from typing import List, Tuple, Set, Dict
import heapq
from utils.profiler import time_this, timed_report
from utils.profiler import ExponentialRange
import itertools
def random_lists_of_chars(n: int) -> List[List[str]]:
characters = string.ascii_uppercase + string.digits
big_list = random.choices(characters, k=n*7)
return [big_list[i:(i+7)] for i in range(n)]
def random_lists_of_floats(n: int) -> List[List[float]]:
return [
[
random.random() for _j in range(7)
] for _i in range(n)
]
@time_this(lambda *args, **kwargs: len(args[0]))
def slow_add_flatten_lists(
the_lists: List[List[str]]) -> List[str]:
"""
Flatten a list of lists via list addition. This has
O(mn^2) compexity for n lists with average length m
"""
result: List[str] = []
for _list in the_lists:
result = result + _list
return result
@time_this(lambda *args, **kwargs: len(args[0]))
def add_flatten_lists(
the_lists: List[List[str]]) -> List[str]:
"""
Flatten a list of lists via list addition
"""
result: List[str] = []
for _list in the_lists:
result += _list
return result
@time_this(lambda *args, **kwargs: len(args[0]))
def extend_flatten_lists(
the_lists: List[List[str]]) -> List[str]:
"""
Flatten a list of lists via extend
"""
result: List[str] = []
for _list in the_lists:
result.extend(_list)
return result
@time_this(lambda *args, **kwargs: len(args[0]))
def append_flatten_lists(
the_lists: List[List[str]]) -> List[str]:
"""
Flatten a list of lists via nested append
"""
result: List[str] = []
for _list in the_lists:
for val in _list:
result.append(val)
return result
@time_this(lambda *args, **kwargs: len(args[0]))
def comprehension_flatten_lists(
the_lists: List[List[str]]) -> List[str]:
"""
Flatten a list of lists via nested list comprehension
"""
return [val for _list in the_lists for val in _list]
@time_this(lambda *args, **kwargs: len(args[0]))
def itertools_flatten_list(
the_lists: List[List[str]]) -> List[str]:
"""
Flatten a list of lists via itertools.chain.from_iterable
"""
return list(itertools.chain.from_iterable(the_lists))
if __name__ == '__main__':
# test_lists = [[1,2,3],[5,6,7],[3,4],[1]]
# print(slow_add_flatten_lists(test_lists))
# print(add_flatten_lists(test_lists))
# print(extend_flatten_lists(test_lists))
# print(append_flatten_lists(test_lists))
# print(comprehension_flatten_lists(test_lists))
exp_range = ExponentialRange(0, 7, 1/4)
the_lists = random_lists_of_chars(exp_range.max)
# the_array = np.array(the_words)
# the_series = pd.Series(the_words)
with timed_report():
for i in exp_range.iterator(4):
slow_add_flatten_lists(the_lists[:i])
for i in exp_range.iterator():
add_flatten_lists(the_lists[:i])
for i in exp_range.iterator():
extend_flatten_lists(the_lists[:i])
for i in exp_range.iterator():
append_flatten_lists(the_lists[:i])
for i in exp_range.iterator():
comprehension_flatten_lists(the_lists[:i])
for i in exp_range.iterator():
itertools_flatten_list(the_lists[:i])