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
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Martin Mareš
Programování 1 pro matematiky
Commits
5bd00cb4
Commit
5bd00cb4
authored
5 years ago
by
Martin Mareš
Browse files
Options
Downloads
Patches
Plain Diff
08: Množiny a slovníky
parent
24b0db21
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
08-slovniky/08-slovniky.tex
+232
-0
232 additions, 0 deletions
08-slovniky/08-slovniky.tex
08-slovniky/Makefile
+3
-0
3 additions, 0 deletions
08-slovniky/Makefile
with
235 additions
and
0 deletions
08-slovniky/08-slovniky.tex
0 → 100644
+
232
−
0
View file @
5bd00cb4
\documentclass
{
beamer
}
\usepackage
[utf8]
{
inputenc
}
\usepackage
[czech]
{
babel
}
\usepackage
{
palatino
}
\usepackage
{
verbatim
}
\usetheme
{
Warsaw
}
\title
{
Programování 1: Množiny a slovníky
}
\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
\def\{
{
\char
123
\relax
}
\def\}
{
\char
125
\relax
}
% ----------------------------------------------------------------------
\begin{frame}
{
Množiny
}
\py
{
%
zvířata =
\{
"pes", "pes", "kočka", "výr"
\}\\
zvířata
}{
%
\{
'výr', 'pes', 'kočka'
\}
}
\py
{
%
"kočka" in zvířata
}{
%
True
}
\py
{
%
"hroznýš" in zvířata
}{
%
False
}
\py
{
%
set(["a", "b", "c"])
}{
%
\{
'c', 'b', 'a'
\}
}
\py
{
%
set("abrakadabra")
}{
%
\{
'r', 'a', 'k', 'b', 'd'
\}
}
\py
{
%
set()
}{
%
set()
\cmt
{
(pozor,
{
\tt
\{\}
}
znamená něco jiného)
}
}
\end{frame}
% ----------------------------------------------------------------------
\begin{frame}
{
Operace s množinami
}
\py
{
%
a=set("abrakadabra")
\\
b=set("popokatepetl")
\\
"".join(sorted(a))
\cmt
{
(umí se chovat jako seznam)
}
}{
%
'abdkr'
}
\py
{
%
k
\&
b
\cmt
{
(průnik)
}
}{
%
\{
'k', 'a'
\}
}
\py
{
%
a | b
\cmt
{
(sjednocení)
}
}{
%
\{
'r','p','a','e','k','b','o','t','d','l'
\}
}
\py
{
%
a - b
\cmt
{
(rozdíl)
}
}{
%
\{
'r', 'd', 'b'
\}
}
\py
{
%
a.remove("r")
\cmt
{
(odebere prvek)
}
\\
b.add("b")
\cmt
{
(přidá prvek)
}
\\
a - b
}{
%
\{
'd'
\}
}
\end{frame}
% ----------------------------------------------------------------------
\begin{frame}
{
Slovníky
}
\py
{
%
teploty =
\{
"Praha": 17, "Dillí": 42, "Longyearbyen": -46
\}\\
teploty["Praha"]
}{
%
17
\cmt
{
(klíčem může být každý neměnný typ)
}
}
\py
{
%
teploty["Horní Dolní"] = 11
\\
del teploty["Horní Dolní"]
\cmt
{
(i pro pole)
}
\\
"Horní Dolní" in teploty
}{
%
False
}
\py
{
%
teploty["Peklo"]
}{
%
<chyba KeyError>
}
\py
{
%
teploty.get("Peklo")
}{
%
None
}
\py
{
%
teploty.get("Peklo", -999)
}{
%
-999
}
\end{frame}
% ----------------------------------------------------------------------
\begin{frame}
{
Cyklus přes prvky slovníku
}
\py
{
%
for k in teploty.keys():
\.\>
print(k)
}{
%
Praha
\\
Dillí
\\
Longyearbyen
}
\py
{
%
for v in teploty.values():
\.\>
print(v)
}{
%
17
\\
42
\\
-46
}
\py
{
%
for k, v in teploty.items():
\.\>
print(f"
\{
k
\}
=
\{
v
\}
")
}{
%
Praha = 17
\\
Dillí = 42
\\
Longyearbyen = -46
}
\end{frame}
% ----------------------------------------------------------------------
\begin{frame}
{
List comprehension pro množiny a slovníky
}
\py
{
%
\{
x**2 for x in range(5)
\}
}{
%
\{
0, 1, 4, 9, 16
\}
}
\py
{
%
\{
x: x**2 for x in range(5)
\}
}{
%
\{
0: 0, 1: 1, 2: 4, 3: 9, 4: 16
\}
}
\end{frame}
% ----------------------------------------------------------------------
\begin{frame}
{
Slovník s~defaulty
}
\py
{
%
from collections import defaultdict
\\
počet = defaultdict(int)
\\
počet['abc']
}{
%
0
}
\py
{
%
for w in "quick brown fox quick".split():
\.\>
počet[w] += 1
\\
list(počet.items())
}{
%
[('abc', 0), ('quick', 2), ('brown', 1), ('fox', 1)]
}
\py
{
%
podle
\_
délek = defaultdict(list)
\\
for w in "quick brown fox".split():
\.\>
podle
\_
délek[len(w)].append(w)
\\
list(podle
\_
délek.items())
}{
%
[(5, ['quick', 'brown']), (3, ['fox'])]
}
\end{frame}
% ----------------------------------------------------------------------
\end{document}
This diff is collapsed.
Click to expand it.
08-slovniky/Makefile
0 → 100644
+
3
−
0
View file @
5bd00cb4
SLIDES
=
08-slovniky.pdf
include
../Makerules
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