Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
Programování 1 pro matematiky
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Martin Mareš
Programování 1 pro matematiky
Commits
e992d97c
Commit
e992d97c
authored
5 years ago
by
Martin Mareš
Browse files
Options
Downloads
Patches
Plain Diff
07: List comprehensions
parent
1bd053d0
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
07-compr/07-compr.tex
+199
-0
199 additions, 0 deletions
07-compr/07-compr.tex
07-compr/Makefile
+3
-0
3 additions, 0 deletions
07-compr/Makefile
07-compr/deleni.py
+4
-0
4 additions, 0 deletions
07-compr/deleni.py
with
206 additions
and
0 deletions
07-compr/07-compr.tex
0 → 100644
+
199
−
0
View file @
e992d97c
\documentclass
{
beamer
}
\usepackage
[utf8]
{
inputenc
}
\usepackage
[czech]
{
babel
}
\usepackage
{
palatino
}
\usepackage
{
verbatim
}
\usetheme
{
Warsaw
}
\title
{
Programování 1: List comprehensions
}
\author
[Martin Mareš]
{
Martin Mareš
\\\texttt
{
mj@ucw.cz
}}
\institute
{
Katedra Aplikované Matematiky
\\
MFF UK Praha
}
\date
{
2019
}
\begin{document}
\setbeamertemplate
{
navigation symbols
}{}
\setbeamertemplate
{
footline
}{}
\setbeamerfont
{
title page
}{
family=
\rmfamily
}
\shorthandoff
{
"
}
\begin{frame}
\titlepage
\end{frame}
\input
../slidemac.tex
% ----------------------------------------------------------------------
\begin{frame}
{
Odbočka: n-tice (tuples)
}
\py
{
%
t = (1, 2, 3)
\cmt
{
(vytváříme trojici)
}
\\
t[1]
\cmt
{
(fungují běžné operace se seznamy)
}
}{
%
2
}
\py
{
%
t[0] = 0
}{
%
<chyba>
\cmt
{
(n-tice nelze měnit)
}
}
\py
{
%
list(t)
}{
%
[1, 2, 3]
\cmt
{
(převedení n-tice na seznam)
}
}
\py
{
%
tuple([4, 5])
}{
%
(4, 5)
\cmt
{
(nebo seznamu na n-tici)
}
}
\py
{
%
s = 11, 22, 33
\\
s
\cmt
{
(více hodnot oddělených čárkou utvoří n-tici)
}
}{
%
(11, 22, 33)
}
\py
{
%
a, b, c = s
\\
b
\cmt
{
(přiřazení každého prvku n-tice/seznamu zvlášť)
}
}{
%
22
}
\end{frame}
% ----------------------------------------------------------------------
\begin{frame}
{
Příklad: Funkce s více výsledky
}
\verbatiminput
{
deleni.py
}
\end{frame}
% ----------------------------------------------------------------------
\begin{frame}
{
List comprehension
}
\py
{
%
seznam=[11, 12, 13, 14, 15]
\\
[a**2 for a in seznam]
}{
%
[121, 144, 169, 196, 225]
}
\py
{
%
[a**2 for a in seznam if a
\%
2 == 1]
}{
%
[121, 169, 225]
}
\py
{
%
[(a,b) for a in [1,2,3] for b in [5,6]]
}{
%
[(1,5), (1,6), (2,5), (2,6), (3,5), (3,6)]
}
\py
{
%
[(a,b) for a in range(4) for b in range(4) if a<b]
}{
%
[(0,1), (0,2), (0,3), (1,2), (1,3), (2,3)]
}
\py
{
%
věta="Kobyla má malý bok"
\\
[len(slovo) for slovo in věta.split()]
}{
%
[6, 2, 4, 3]
}
\end{frame}
% ----------------------------------------------------------------------
\begin{frame}
{
List comprehension: Další triky
}
\py
{
%
[int(s) for s in input().split()]
\\
1 2 3 4 5
\cmt
{
(načtení seznamu čísel na jednom řádku)
}
}{
%
[1, 2, 3, 4, 5]
}
\py
{
%
s=[[1, 2, 3], [4, 5]]
\\
[x for t in s for x in t]
}{
%
[1, 2, 3, 4, 5]
\cmt
{
(
\uv
{
zploštění
}
seznamu)
}
}
\py
{
%
m=[[0]*5 for
\_
in range(3)]
\\
m
\cmt
{
(výroba matice: seznam 3 různých seznamů nul)
}
}{
%
[[0,0,0,0,0], [0,0,0,0,0], [0,0,0,0,0]]
}
\py
{
%
n=[[1, 2, 3], [4, 5, 6]]
\\
[[r[i] for r in n] for i in range(3)]
}{
%
[[1,4], [2,5], [3,6]]
\cmt
{
(transpozice matice)
}
}
\end{frame}
% ----------------------------------------------------------------------
\begin{frame}
{
Agregační funkce
}
\py
{
%
s = [2**x
\%
37 for x in range(10)]
\\
s
}{
%
[1, 2, 4, 8, 16, 32, 27, 17, 34, 31]
}
\py
{
%
min(s)
}{
%
1
}
\py
{
%
max(s)
}{
%
34
}
\py
{
%
sum(s)
}{
%
172
}
\py
{
%
all([x < 37 for x in s])
}{
%
True
\cmt
{
(and přes všechny prvky seznamu)
}
}
\py
{
%
any([x > 33 for x in s])
}{
%
True
\cmt
{
(or přes všechny prvky seznamu)
}
}
\py
{
%
sorted(s)
}{
%
[1, 2, 4, 8, 16, 17, 27, 31, 32, 34]
}
\end{frame}
% ----------------------------------------------------------------------
\end{document}
This diff is collapsed.
Click to expand it.
07-compr/Makefile
0 → 100644
+
3
−
0
View file @
e992d97c
SLIDES
=
07-compr.pdf
include
../Makerules
This diff is collapsed.
Click to expand it.
07-compr/deleni.py
0 → 100644
+
4
−
0
View file @
e992d97c
def
deleni
(
x
,
y
):
return
x
//
y
,
x
%
y
podil
,
zbytek
=
deleni
(
1000
,
7
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment