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 ffca067

Browse files
committed
finish zip()
1 parent 46cefdd commit ffca067

File tree

3 files changed

+296
-0
lines changed

3 files changed

+296
-0
lines changed
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# zip()\n",
8+
"\n",
9+
"Make an iterator that aggregates elements from each of the iterables.\n",
10+
"\n",
11+
"Returns an iterator of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables. The iterator stops when the shortest input iterable is exhausted. With a single iterable argument, it returns an iterator of 1-tuples. With no arguments, it returns an empty iterator."
12+
]
13+
},
14+
{
15+
"cell_type": "code",
16+
"execution_count": 7,
17+
"metadata": {},
18+
"outputs": [
19+
{
20+
"name": "stdout",
21+
"output_type": "stream",
22+
"text": [
23+
"[(1, 4), (2, 5), (3, 6)]\n"
24+
]
25+
}
26+
],
27+
"source": [
28+
"x = [1, 2, 3]\n",
29+
"y = [4, 5, 6]\n",
30+
"zipped_list = list(zip(x, y))\n",
31+
"print(zipped_list)"
32+
]
33+
},
34+
{
35+
"cell_type": "markdown",
36+
"metadata": {},
37+
"source": [
38+
"This extends to as many iterables as you want:"
39+
]
40+
},
41+
{
42+
"cell_type": "code",
43+
"execution_count": 19,
44+
"metadata": {},
45+
"outputs": [
46+
{
47+
"name": "stdout",
48+
"output_type": "stream",
49+
"text": [
50+
"[(1, 4, 7, 10), (2, 5, 8, 11), (3, 6, 9, 12)]\n"
51+
]
52+
}
53+
],
54+
"source": [
55+
"x = [1, 2, 3]\n",
56+
"y = [4, 5, 6]\n",
57+
"z = [7, 8, 9]\n",
58+
"w = [10, 11, 12]\n",
59+
"zipped_list = list(zip(x, y, z, w))\n",
60+
"print(zipped_list)"
61+
]
62+
},
63+
{
64+
"cell_type": "markdown",
65+
"metadata": {},
66+
"source": [
67+
"`zip()` should only be used with unequal length inputs when you don’t care about trailing, unmatched values from the longer iterables. If those values are important, use `itertools.zip_longest()` instead.\n",
68+
"\n",
69+
"`zip()` in conjunction with the `*` operator can be used to unzip a list:"
70+
]
71+
},
72+
{
73+
"cell_type": "code",
74+
"execution_count": 23,
75+
"metadata": {},
76+
"outputs": [
77+
{
78+
"name": "stdout",
79+
"output_type": "stream",
80+
"text": [
81+
"[(1, 4), (2, 5), (3, 6)] \n",
82+
"\n",
83+
"(1, 2, 3)\n",
84+
"(4, 5, 6)\n"
85+
]
86+
}
87+
],
88+
"source": [
89+
"x = [1, 2, 3]\n",
90+
"y = [4, 5, 6]"
91+
]
92+
},
93+
{
94+
"cell_type": "markdown",
95+
"metadata": {},
96+
"source": [
97+
"You can also use `zip(key_list, value_list)` to easily create dictionaries from a pair of lists:"
98+
]
99+
},
100+
{
101+
"cell_type": "code",
102+
"execution_count": 28,
103+
"metadata": {},
104+
"outputs": [
105+
{
106+
"name": "stdout",
107+
"output_type": "stream",
108+
"text": [
109+
"{'Peter Parker': 'Spiderman', 'Tony Stark': 'Iron Man', 'Clark Kent': 'Superman'} \n",
110+
"\n",
111+
"dict_keys(['Peter Parker', 'Tony Stark', 'Clark Kent'])\n",
112+
"dict_values(['Spiderman', 'Iron Man', 'Superman'])\n"
113+
]
114+
}
115+
],
116+
"source": [
117+
"names = [\"Peter Parker\", \"Tony Stark\", \"Clark Kent\"]\n",
118+
"aliases = [\"Spiderman\", \"Iron Man\", \"Superman\"]\n",
119+
"\n",
120+
"superheroes = dict(zip(names, aliases))\n",
121+
"print(superheroes, '\\n')\n",
122+
"print(superheroes.keys())\n",
123+
"print(superheroes.values())"
124+
]
125+
}
126+
],
127+
"metadata": {
128+
"kernelspec": {
129+
"display_name": "Python 3",
130+
"language": "python",
131+
"name": "python3"
132+
},
133+
"language_info": {
134+
"codemirror_mode": {
135+
"name": "ipython",
136+
"version": 3
137+
},
138+
"file_extension": ".py",
139+
"mimetype": "text/x-python",
140+
"name": "python",
141+
"nbconvert_exporter": "python",
142+
"pygments_lexer": "ipython3",
143+
"version": "3.6.4"
144+
}
145+
},
146+
"nbformat": 4,
147+
"nbformat_minor": 2
148+
}

‎built_in_functions/zip.ipynb

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# zip()\n",
8+
"\n",
9+
"Make an iterator that aggregates elements from each of the iterables.\n",
10+
"\n",
11+
"Returns an iterator of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables. The iterator stops when the shortest input iterable is exhausted. With a single iterable argument, it returns an iterator of 1-tuples. With no arguments, it returns an empty iterator."
12+
]
13+
},
14+
{
15+
"cell_type": "code",
16+
"execution_count": 7,
17+
"metadata": {},
18+
"outputs": [
19+
{
20+
"name": "stdout",
21+
"output_type": "stream",
22+
"text": [
23+
"[(1, 4), (2, 5), (3, 6)]\n"
24+
]
25+
}
26+
],
27+
"source": [
28+
"x = [1, 2, 3]\n",
29+
"y = [4, 5, 6]\n",
30+
"zipped_list = list(zip(x, y))\n",
31+
"print(zipped_list)"
32+
]
33+
},
34+
{
35+
"cell_type": "markdown",
36+
"metadata": {},
37+
"source": [
38+
"This extends to as many iterables as you want:"
39+
]
40+
},
41+
{
42+
"cell_type": "code",
43+
"execution_count": 19,
44+
"metadata": {},
45+
"outputs": [
46+
{
47+
"name": "stdout",
48+
"output_type": "stream",
49+
"text": [
50+
"[(1, 4, 7, 10), (2, 5, 8, 11), (3, 6, 9, 12)]\n"
51+
]
52+
}
53+
],
54+
"source": [
55+
"x = [1, 2, 3]\n",
56+
"y = [4, 5, 6]\n",
57+
"z = [7, 8, 9]\n",
58+
"w = [10, 11, 12]\n",
59+
"zipped_list = list(zip(x, y, z, w))\n",
60+
"print(zipped_list)"
61+
]
62+
},
63+
{
64+
"cell_type": "markdown",
65+
"metadata": {},
66+
"source": [
67+
"`zip()` should only be used with unequal length inputs when you don’t care about trailing, unmatched values from the longer iterables. If those values are important, use `itertools.zip_longest()` instead.\n",
68+
"\n",
69+
"`zip()` in conjunction with the `*` operator can be used to unzip a list:"
70+
]
71+
},
72+
{
73+
"cell_type": "code",
74+
"execution_count": 23,
75+
"metadata": {},
76+
"outputs": [
77+
{
78+
"name": "stdout",
79+
"output_type": "stream",
80+
"text": [
81+
"[(1, 4), (2, 5), (3, 6)] \n",
82+
"\n",
83+
"(1, 2, 3)\n",
84+
"(4, 5, 6)\n"
85+
]
86+
}
87+
],
88+
"source": [
89+
"x = [1, 2, 3]\n",
90+
"y = [4, 5, 6]"
91+
]
92+
},
93+
{
94+
"cell_type": "markdown",
95+
"metadata": {},
96+
"source": [
97+
"You can also use `zip(key_list, value_list)` to easily create dictionaries from a pair of lists:"
98+
]
99+
},
100+
{
101+
"cell_type": "code",
102+
"execution_count": 28,
103+
"metadata": {},
104+
"outputs": [
105+
{
106+
"name": "stdout",
107+
"output_type": "stream",
108+
"text": [
109+
"{'Peter Parker': 'Spiderman', 'Tony Stark': 'Iron Man', 'Clark Kent': 'Superman'} \n",
110+
"\n",
111+
"dict_keys(['Peter Parker', 'Tony Stark', 'Clark Kent'])\n",
112+
"dict_values(['Spiderman', 'Iron Man', 'Superman'])\n"
113+
]
114+
}
115+
],
116+
"source": [
117+
"names = [\"Peter Parker\", \"Tony Stark\", \"Clark Kent\"]\n",
118+
"aliases = [\"Spiderman\", \"Iron Man\", \"Superman\"]\n",
119+
"\n",
120+
"superheroes = dict(zip(names, aliases))\n",
121+
"print(superheroes, '\\n')\n",
122+
"print(superheroes.keys())\n",
123+
"print(superheroes.values())"
124+
]
125+
}
126+
],
127+
"metadata": {
128+
"kernelspec": {
129+
"display_name": "Python 3",
130+
"language": "python",
131+
"name": "python3"
132+
},
133+
"language_info": {
134+
"codemirror_mode": {
135+
"name": "ipython",
136+
"version": 3
137+
},
138+
"file_extension": ".py",
139+
"mimetype": "text/x-python",
140+
"name": "python",
141+
"nbconvert_exporter": "python",
142+
"pygments_lexer": "ipython3",
143+
"version": "3.6.4"
144+
}
145+
},
146+
"nbformat": 4,
147+
"nbformat_minor": 2
148+
}

‎built_in_functions/zip.py

Whitespace-only changes.

0 commit comments

Comments
(0)

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