Skip to content

Commit 8ddf635

Browse files
committed
consistency updates
1 parent 0138df3 commit 8ddf635

File tree

4 files changed

+21
-21
lines changed

4 files changed

+21
-21
lines changed

sort-python-dictionary/compare_lambda_vs_getter.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,10 @@
3737
)
3838

3939
print(
40-
f"""
40+
f"""\
4141
{sorted_with_lambda_time=:.2f} seconds
4242
{sorted_with_itemgetter_time=:.2f} seconds
4343
itemgetter is {(
4444
sorted_with_lambda_time / sorted_with_itemgetter_time
45-
):.2f} times faster
46-
""".strip()
45+
):.2f} times faster"""
4746
)
Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,28 @@
11
from timeit import timeit
22

3-
list_program = "sorted(list_of_dictionaries, key=lambda item:item['age'])"
4-
dict_program = """
3+
sorting_list = "sorted(list_of_dictionaries, key=lambda item:item['age'])"
4+
sorting_dict = """
55
dict(
66
sorted(
77
dictionary_of_dictionaries.items(), key=lambda item: item[1]['age']
88
)
99
)
1010
"""
1111

12-
list_time = timeit(
13-
stmt=list_program,
12+
sorting_list_time = timeit(
13+
stmt=sorting_list,
1414
setup="from samples import list_of_dictionaries",
1515
globals=globals(),
1616
)
17-
dict_time = timeit(
18-
stmt=dict_program,
17+
sorting_dict_time = timeit(
18+
stmt=sorting_dict,
1919
setup="from samples import dictionary_of_dictionaries",
2020
globals=globals(),
2121
)
2222

2323
print(
24-
f"""
25-
{list_time=:.2f} seconds
26-
{dict_time=:.2f} seconds
27-
list is {(dict_time/list_time):.2f} times faster
28-
"""
24+
f"""\
25+
{sorting_list_time=:.2f} seconds
26+
{sorting_dict_time=:.2f} seconds
27+
list is {(sorting_dict_time/sorting_list_time):.2f} times faster"""
2928
)

sort-python-dictionary/nested_sort.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@ def get_python(item):
2323
print(sorted(data.items(), key=get_python, reverse=True))
2424

2525
# Doing the same thing with lambda function and conditional expression
26-
sorted(
27-
data.items(),
28-
key=lambda item: item[1]["skills"]["python"]
29-
if "python" in item[1]["skills"]
30-
else 0,
31-
reverse=True,
26+
print(
27+
sorted(
28+
data.items(),
29+
key=lambda item: item[1]["skills"]["python"]
30+
if "python" in item[1]["skills"]
31+
else 0,
32+
reverse=True,
33+
)
3234
)

sort-python-dictionary/sorting_a_dictionary.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Sorting lists with the sorted() function
1+
# Reviewing the sorted() function by sorting a list
22
numbers = [5, 3, 4, 3, 6, 7, 3, 2, 3, 4, 1]
33
sorted(numbers)
44
words = ["aa", "ab", "ac", "ba", "cb", "ca"]

0 commit comments

Comments
 (0)