Skip to content

Commit c5ae8bb

Browse files
j3r3miashuangsam
andauthored
defaultdict data structure. (huangsam#105)
* defaultdict data structure. * E201. * Using an EPS value to compare floats. * E302. * READMEs. --------- Co-authored-by: Samuel Huang <samhuang91@gmail.com>
1 parent e81ab1d commit c5ae8bb

File tree

6 files changed

+62
-0
lines changed

6 files changed

+62
-0
lines changed

README.de.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ Es gibt zwei Möglichkeiten, die Module auszuführen:
9292
- String: [String operations](ultimatepython/data_structures/string.py) (:cake:)
9393
- Deque: [deque](ultimatepython/data_structures/deque.py) (:exploding_head:)
9494
- Namedtuple: [namedtuple](ultimatepython/data_structures/namedtuple.py) (:exploding_head:)
95+
- Defaultdict: [defaultdict](ultimatepython/data_structures/defaultdict.py) (:exploding_head:)
9596
- Time complexity: [cPython operations](https://wiki.python.org/moin/TimeComplexity) (:books:, :exploding_head:)
9697
4. **Klassen**
9798
- Basic class: [Basic definition](ultimatepython/classes/basic_class.py) (:cake:)

README.es.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ Hay dos maneras de ejecutar los módulos:
9191
- Cadena: [Operaciones con strings](ultimatepython/data_structures/string.py) (:cake:)
9292
- Deque: [deque](ultimatepython/data_structures/deque.py) (:exploding_head:)
9393
- Namedtuple: [namedtuple](ultimatepython/data_structures/namedtuple.py) (:exploding_head:)
94+
- Defaultdict: [defaultdict](ultimatepython/data_structures/defaultdict.py) (:exploding_head:)
9495
- Complejidad de tiempo: [Operaciones de cPython](https://wiki.python.org/moin/TimeComplexity) (:books:, :exploding_head:)
9596
4. **Clases**
9697
- Clase básica: [Definición de básica](ultimatepython/classes/basic_class.py) (:cake:)

README.ko.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ Repl.it와 같은 브라우저에서 실행할 수있는 독립형 모듈 모음
8080
- 문자열 : [문자열 연산](ultimatepython/data_structures/string.py) (:cake:)
8181
- Deque: [deque](ultimatepython/data_structures/deque.py) (:exploding_head:)
8282
- Namedtuple: [namedtuple](ultimatepython/data_structures/namedtuple.py) (:exploding_head:)
83+
- Defaultdict: [defaultdict](ultimatepython/data_structures/defaultdict.py) (:exploding_head:)
8384
- 시간 복잡성 : [cPython 작업](https://wiki.python.org/moin/TimeComplexity) (:books:, :exploding_head:)
8485
4. **클래스**
8586
- 기본 클래스 : [기본 정의](ultimatepython/classes/basic_class.py) (:cake:)

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ There are two ways of running the modules:
9393
- String: [String operations](ultimatepython/data_structures/string.py) (:cake:)
9494
- Deque: [deque](ultimatepython/data_structures/deque.py) (:exploding_head:)
9595
- Namedtuple: [namedtuple](ultimatepython/data_structures/namedtuple.py) (:exploding_head:)
96+
- Defaultdict: [defaultdict](ultimatepython/data_structures/defaultdict.py) (:exploding_head:)
9697
- Time complexity: [cPython operations](https://wiki.python.org/moin/TimeComplexity) (:books:, :exploding_head:)
9798
4. **Classes**
9899
- Basic class: [Basic definition](ultimatepython/classes/basic_class.py) (:cake:)

README.zh_tw.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ print("Ultimate Python 學習大綱")
7676
- 字串:[字串操作](ultimatepython/data_structures/string.py) (:cake:)
7777
- 雙端隊列:[deque](ultimatepython/data_structures/deque.py) (:exploding_head:)
7878
- Namedtuple: [namedtuple](ultimatepython/data_structures/namedtuple.py) (:exploding_head:)
79+
- Defaultdict: [defaultdict](ultimatepython/data_structures/defaultdict.py) (:exploding_head:)
7980
- 時間複雜度:[cPython操作](https://wiki.python.org/moin/TimeComplexity) (:books:, :exploding_head:)
8081
4. **類別**
8182
- 基本類別:[基本定義](ultimatepython/classes/basic_class.py) (:cake:)
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
"""
2+
This module demonstrates the use of defaultdict, which is a dictionary that is
3+
possible to setup a default value in its creation.
4+
"""
5+
6+
from collections import defaultdict
7+
8+
# Module-level constants
9+
_GPA_MIN = 0.0
10+
_GPA_MAX = 4.0
11+
_EPS = 0.000001
12+
13+
14+
def main():
15+
# Let's create a defaultdict with student keys and GPA values. The first
16+
# parameter is called default_factory and it is the initialization value for
17+
# first use of a key. It can be a common type or a function
18+
student_gpa = defaultdict(float, [("john", 3.5), ("bob", 2.8), ("mary", 3.2)])
19+
20+
# There are three student records in this dictionary
21+
assert len(student_gpa) == 3
22+
23+
# Each student has a name key and a GPA value
24+
assert len(student_gpa.keys()) == len(student_gpa.values())
25+
26+
# We can get the names in isolation. Note that in Python 3.7 and
27+
# above, dictionary entries are sorted in the order that they were
28+
# defined or inserted
29+
student_names = []
30+
for student in student_gpa.keys():
31+
student_names.append(student)
32+
assert student_names == ["john", "bob", "mary"]
33+
34+
# We can get the GPA for a specific student
35+
assert abs(student_gpa["john"] < 3.5) < _EPS
36+
37+
# And the defaultdict allow us to get the GPA of a student that is not in
38+
# the data structure yet, returning a default value for float that is 0.0
39+
assert student_gpa["jane"] == _GPA_MIN
40+
41+
# And now there are four student records in this dictionary
42+
assert len(student_gpa) == 4
43+
44+
# You can set the default value in default_factory attribute
45+
def set_default_to_gpa_max():
46+
return _GPA_MAX
47+
48+
student_gpa.default_factory = set_default_to_gpa_max
49+
50+
assert student_gpa["rika"] == _GPA_MAX
51+
52+
# And now there are five student records in this dictionary
53+
assert len(student_gpa) == 5
54+
55+
56+
if __name__ == "__main__":
57+
main()

0 commit comments

Comments
 (0)