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 e153603

Browse files
updates
1 parent 7ce7d31 commit e153603

File tree

1 file changed

+225
-14
lines changed

1 file changed

+225
-14
lines changed

‎python3_helpful_coding_patterns_&_features.ipynb

Lines changed: 225 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -681,13 +681,139 @@
681681
},
682682
{
683683
"cell_type": "code",
684-
"execution_count": 1,
684+
"execution_count": 2,
685685
"metadata": {},
686686
"outputs": [],
687687
"source": [
688688
"import numpy as np"
689689
]
690690
},
691+
{
692+
"cell_type": "markdown",
693+
"metadata": {},
694+
"source": [
695+
"## Looping Over Arrays"
696+
]
697+
},
698+
{
699+
"cell_type": "markdown",
700+
"metadata": {},
701+
"source": [
702+
"You can directly loop over rows of an array easily, without using indices:"
703+
]
704+
},
705+
{
706+
"cell_type": "code",
707+
"execution_count": 37,
708+
"metadata": {},
709+
"outputs": [
710+
{
711+
"data": {
712+
"text/plain": [
713+
"array([[1, 2, 3],\n",
714+
" [4, 5, 6],\n",
715+
" [7, 8, 9]])"
716+
]
717+
},
718+
"execution_count": 37,
719+
"metadata": {},
720+
"output_type": "execute_result"
721+
}
722+
],
723+
"source": [
724+
"my_array_2d = np.array([1,2,3,4,5,6,7,8,9]).reshape(3, 3)\n",
725+
"my_array_2d"
726+
]
727+
},
728+
{
729+
"cell_type": "code",
730+
"execution_count": 38,
731+
"metadata": {},
732+
"outputs": [
733+
{
734+
"name": "stdout",
735+
"output_type": "stream",
736+
"text": [
737+
"[1 2 3]\n",
738+
"[4 5 6]\n",
739+
"[7 8 9]\n"
740+
]
741+
}
742+
],
743+
"source": [
744+
"for row in my_array_2d:\n",
745+
" print(row)"
746+
]
747+
},
748+
{
749+
"cell_type": "markdown",
750+
"metadata": {},
751+
"source": [
752+
"To loop over columns, just take the transpose using `.T` and loop over the rows of the transpose."
753+
]
754+
},
755+
{
756+
"cell_type": "code",
757+
"execution_count": 39,
758+
"metadata": {
759+
"scrolled": true
760+
},
761+
"outputs": [
762+
{
763+
"name": "stdout",
764+
"output_type": "stream",
765+
"text": [
766+
"[1 4 7]\n",
767+
"[2 5 8]\n",
768+
"[3 6 9]\n"
769+
]
770+
}
771+
],
772+
"source": [
773+
"for col in my_array_2d.T:\n",
774+
" print(col)"
775+
]
776+
},
777+
{
778+
"cell_type": "markdown",
779+
"metadata": {},
780+
"source": [
781+
"### np.ndenumerate"
782+
]
783+
},
784+
{
785+
"cell_type": "markdown",
786+
"metadata": {},
787+
"source": [
788+
"`np.ndenumerate` allows looping over the array and enumerating indices in a concise, clean manner."
789+
]
790+
},
791+
{
792+
"cell_type": "code",
793+
"execution_count": 44,
794+
"metadata": {},
795+
"outputs": [
796+
{
797+
"name": "stdout",
798+
"output_type": "stream",
799+
"text": [
800+
"Index: (0, 0), Value: 1\n",
801+
"Index: (0, 1), Value: 2\n",
802+
"Index: (0, 2), Value: 3\n",
803+
"Index: (1, 0), Value: 4\n",
804+
"Index: (1, 1), Value: 5\n",
805+
"Index: (1, 2), Value: 6\n",
806+
"Index: (2, 0), Value: 7\n",
807+
"Index: (2, 1), Value: 8\n",
808+
"Index: (2, 2), Value: 9\n"
809+
]
810+
}
811+
],
812+
"source": [
813+
"for index, value in np.ndenumerate(my_array_2d):\n",
814+
" print('Index: {}, Value: {}'.format(index, value))"
815+
]
816+
},
691817
{
692818
"cell_type": "markdown",
693819
"metadata": {},
@@ -842,7 +968,7 @@
842968
"cell_type": "markdown",
843969
"metadata": {},
844970
"source": [
845-
"## Array Rotation"
971+
"## Array Rolling with np.roll"
846972
]
847973
},
848974
{
@@ -862,7 +988,9 @@
862988
"\n",
863989
"E.g.:\n",
864990
"\n",
865-
"Rotate the array `right` by `2` positions: Expected Result = ` 4 | 5 | 1 | 2 | 3 `."
991+
"Rotate the array `right` by `2` positions: Expected Result = ` 4 | 5 | 1 | 2 | 3 `.\n",
992+
"\n",
993+
"For a 1-d list, it can be done manually in a one-liner easily, but in an n-dim array, rolling along the mth dim can be easily done by `np.roll`"
866994
]
867995
},
868996
{
@@ -914,17 +1042,17 @@
9141042
},
9151043
{
9161044
"cell_type": "code",
917-
"execution_count": 8,
1045+
"execution_count": 5,
9181046
"metadata": {},
9191047
"outputs": [],
9201048
"source": [
9211049
"# multi-dimenisional cases\n",
922-
"my_array_nd = np.array([1,2,3,4,5,6,7,8,9]).reshape(3, 3)"
1050+
"my_array_2d = np.array([1,2,3,4,5,6,7,8,9]).reshape(3, 3)"
9231051
]
9241052
},
9251053
{
9261054
"cell_type": "code",
927-
"execution_count": 14,
1055+
"execution_count": 6,
9281056
"metadata": {},
9291057
"outputs": [
9301058
{
@@ -939,12 +1067,12 @@
9391067
}
9401068
],
9411069
"source": [
942-
"print('original 2-d array: \\n', my_array_nd)"
1070+
"print('original 2-d array: \\n', my_array_2d)"
9431071
]
9441072
},
9451073
{
9461074
"cell_type": "code",
947-
"execution_count": 16,
1075+
"execution_count": 7,
9481076
"metadata": {},
9491077
"outputs": [
9501078
{
@@ -959,16 +1087,60 @@
9591087
}
9601088
],
9611089
"source": [
962-
"my_array_nd_rolled = np.roll(my_array_nd, 1, axis=0)\n",
963-
"print('rolled 2-d array: \\n', my_array_nd_rolled)"
1090+
"my_array_2d_rolled = np.roll(my_array_2d, 1, axis=0)\n",
1091+
"print('rolled 2-d array: \\n', my_array_2d_rolled)"
1092+
]
1093+
},
1094+
{
1095+
"cell_type": "markdown",
1096+
"metadata": {},
1097+
"source": [
1098+
"## Array Rotation"
1099+
]
1100+
},
1101+
{
1102+
"cell_type": "code",
1103+
"execution_count": 19,
1104+
"metadata": {},
1105+
"outputs": [
1106+
{
1107+
"data": {
1108+
"text/plain": [
1109+
"array([[1, 2, 3],\n",
1110+
" [4, 5, 6],\n",
1111+
" [7, 8, 9]])"
1112+
]
1113+
},
1114+
"execution_count": 19,
1115+
"metadata": {},
1116+
"output_type": "execute_result"
1117+
}
1118+
],
1119+
"source": [
1120+
"my_array_2d"
9641121
]
9651122
},
9661123
{
9671124
"cell_type": "code",
968-
"execution_count": null,
1125+
"execution_count": 26,
9691126
"metadata": {},
970-
"outputs": [],
971-
"source": []
1127+
"outputs": [
1128+
{
1129+
"data": {
1130+
"text/plain": [
1131+
"array([[7, 4, 1],\n",
1132+
" [8, 5, 2],\n",
1133+
" [9, 6, 3]])"
1134+
]
1135+
},
1136+
"execution_count": 26,
1137+
"metadata": {},
1138+
"output_type": "execute_result"
1139+
}
1140+
],
1141+
"source": [
1142+
"np.rot90(my_array_2d, k=-1)"
1143+
]
9721144
},
9731145
{
9741146
"cell_type": "code",
@@ -994,7 +1166,46 @@
9941166
"name": "python",
9951167
"nbconvert_exporter": "python",
9961168
"pygments_lexer": "ipython3",
997-
"version": "3.6.5"
1169+
"version": "3.6.4"
1170+
},
1171+
"toc": {
1172+
"nav_menu": {},
1173+
"number_sections": true,
1174+
"sideBar": true,
1175+
"skip_h1_title": false,
1176+
"toc_cell": false,
1177+
"toc_position": {},
1178+
"toc_section_display": "block",
1179+
"toc_window_display": false
1180+
},
1181+
"varInspector": {
1182+
"cols": {
1183+
"lenName": 16,
1184+
"lenType": 16,
1185+
"lenVar": 40
1186+
},
1187+
"kernels_config": {
1188+
"python": {
1189+
"delete_cmd_postfix": "",
1190+
"delete_cmd_prefix": "del ",
1191+
"library": "var_list.py",
1192+
"varRefreshCmd": "print(var_dic_list())"
1193+
},
1194+
"r": {
1195+
"delete_cmd_postfix": ") ",
1196+
"delete_cmd_prefix": "rm(",
1197+
"library": "var_list.r",
1198+
"varRefreshCmd": "cat(var_dic_list()) "
1199+
}
1200+
},
1201+
"types_to_exclude": [
1202+
"module",
1203+
"function",
1204+
"builtin_function_or_method",
1205+
"instance",
1206+
"_Feature"
1207+
],
1208+
"window_display": false
9981209
}
9991210
},
10001211
"nbformat": 4,

0 commit comments

Comments
(0)

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