Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 63b6290

Browse files
added defaultdict section
1 parent 59a44c6 commit 63b6290

File tree

1 file changed

+146
-0
lines changed

1 file changed

+146
-0
lines changed

‎python3_helpful_coding_patterns_&_features.ipynb

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,152 @@
617617
"This solution has only one loop, and with the `get()` method being $O(1),ドル the total time complexity for this solution is $O(n)$."
618618
]
619619
},
620+
{
621+
"cell_type": "markdown",
622+
"metadata": {},
623+
"source": [
624+
"## `defaultdict()`"
625+
]
626+
},
627+
{
628+
"cell_type": "code",
629+
"execution_count": 1,
630+
"metadata": {
631+
"ExecuteTime": {
632+
"end_time": "2018年06月24日T08:24:02.341531Z",
633+
"start_time": "2018年06月24日T08:24:02.335671Z"
634+
}
635+
},
636+
"outputs": [],
637+
"source": [
638+
"from collections import defaultdict"
639+
]
640+
},
641+
{
642+
"cell_type": "markdown",
643+
"metadata": {},
644+
"source": [
645+
"To initialize a defaultdict with an integer value (`0`):"
646+
]
647+
},
648+
{
649+
"cell_type": "code",
650+
"execution_count": 2,
651+
"metadata": {
652+
"ExecuteTime": {
653+
"end_time": "2018年06月24日T08:25:02.712379Z",
654+
"start_time": "2018年06月24日T08:25:02.682608Z"
655+
}
656+
},
657+
"outputs": [
658+
{
659+
"data": {
660+
"text/plain": [
661+
"0"
662+
]
663+
},
664+
"execution_count": 2,
665+
"metadata": {},
666+
"output_type": "execute_result"
667+
}
668+
],
669+
"source": [
670+
"d = defaultdict(int)\n",
671+
"d['test']"
672+
]
673+
},
674+
{
675+
"cell_type": "markdown",
676+
"metadata": {},
677+
"source": [
678+
"To initialize a defaultdict with a custom value, other than `0`:"
679+
]
680+
},
681+
{
682+
"cell_type": "code",
683+
"execution_count": 3,
684+
"metadata": {
685+
"ExecuteTime": {
686+
"end_time": "2018年06月24日T08:25:36.973182Z",
687+
"start_time": "2018年06月24日T08:25:36.962071Z"
688+
}
689+
},
690+
"outputs": [
691+
{
692+
"data": {
693+
"text/plain": [
694+
"17"
695+
]
696+
},
697+
"execution_count": 3,
698+
"metadata": {},
699+
"output_type": "execute_result"
700+
}
701+
],
702+
"source": [
703+
"d = defaultdict(lambda : 17)\n",
704+
"d['test']"
705+
]
706+
},
707+
{
708+
"cell_type": "markdown",
709+
"metadata": {},
710+
"source": [
711+
"The idea here is that you need to pass a callable function to `defaultdict()` that outputs the default value when the key is not present in the dict."
712+
]
713+
},
714+
{
715+
"cell_type": "code",
716+
"execution_count": 4,
717+
"metadata": {
718+
"ExecuteTime": {
719+
"end_time": "2018年06月24日T08:26:42.616985Z",
720+
"start_time": "2018年06月24日T08:26:42.606931Z"
721+
}
722+
},
723+
"outputs": [
724+
{
725+
"data": {
726+
"text/plain": [
727+
"0"
728+
]
729+
},
730+
"execution_count": 4,
731+
"metadata": {},
732+
"output_type": "execute_result"
733+
}
734+
],
735+
"source": [
736+
"int() # note that calling int() returns 0"
737+
]
738+
},
739+
{
740+
"cell_type": "code",
741+
"execution_count": 6,
742+
"metadata": {
743+
"ExecuteTime": {
744+
"end_time": "2018年06月24日T08:27:30.372964Z",
745+
"start_time": "2018年06月24日T08:27:30.362136Z"
746+
}
747+
},
748+
"outputs": [
749+
{
750+
"data": {
751+
"text/plain": [
752+
"[1, 10, 100]"
753+
]
754+
},
755+
"execution_count": 6,
756+
"metadata": {},
757+
"output_type": "execute_result"
758+
}
759+
],
760+
"source": [
761+
"# one more example\n",
762+
"d = defaultdict(lambda : [1, 10, 100])\n",
763+
"d['test']"
764+
]
765+
},
620766
{
621767
"cell_type": "markdown",
622768
"metadata": {},

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /