Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Python: Listas, Dicionarios, Sets e outros mons...
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
fractal
October 07, 2011
Programming
110
2
Share
Python: Listas, Dicionarios, Sets e outros monstros mitológicos
Uma olhada em baixo do capô do python de como as estruturas básicas se comportam
fractal
October 07, 2011
Other Decks in Programming
See All in Programming
Strategy for Finding a Problem for OSS: With Real Examples
kibitan
0
130
KagglerがMixSeekを触ってみた
morim
0
360
CS教育のDX AIによる育成の効率化
niftycorp
PRO
0
190
RSAが破られる前に知っておきたい 耐量子計算機暗号(PQC)入門 / Intro to PQC: Preparing for the Post-RSA Era
mackey0225
3
120
Goの型安全性で実現する複数プロダクトの権限管理
ishikawa_pro
2
1.4k
AIと共にエンジニアとPMの “二刀流”を実現する
naruogram
0
120
Ruby and LLM Ecosystem 2nd
koic
1
1.4k
へんな働き方
yusukebe
6
2.9k
PHP 7.4でもOpenTelemetryゼロコード計装がしたい! / PHPerKaigi 2026
arthur1
1
480
煩雑なSkills管理をSoC(関心の分離)により解決する――関心を分離し、プロンプトを部品として育てるためのOSSを作った話 / Solving Complex Skills Management Through SoC (Separation of Concerns)
nrslib
3
210
飯MCP
yusukebe
0
470
AI-DLC 入門 〜AIコーディングの本質は「コード」ではなく「構造」〜 / Introduction to AI-DLC: The Essence of AI Coding Is Not “Code” but “Structure”
seike460
PRO
0
210
Featured
See All Featured
How to Think Like a Performance Engineer
csswizardry
28
2.5k
Producing Creativity
orderedlist
PRO
348
40k
The Cult of Friendly URLs
andyhume
79
6.8k
The SEO identity crisis: Don't let AI make you average
varn
0
430
Self-Hosted WebAssembly Runtime for Runtime-Neutral Checkpoint/Restore in Edge–Cloud Continuum
chikuwait
0
440
How to Get Subject Matter Experts Bought In and Actively Contributing to SEO & PR Initiatives.
livdayseo
0
94
AI in Enterprises - Java and Open Source to the Rescue
ivargrimstad
0
1.2k
Bash Introduction
62gerente
615
210k
Chasing Engaging Ingredients in Design
codingconduct
0
160
StorybookのUI Testing Handbookを読んだ
zakiyama
31
6.7k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
38
2.8k
Balancing Empowerment & Direction
lara
5
1k
Transcript
Listas, Dicionários, Sets e outras Bestas Mitológicas --- Adriano Petrich
O(n)?
Listas
Prós: Fantásticas
Listas l = []
L.append(foo) é O(1)
L.pop() é O(1)
Tá Isso não é tão fantástico
L[foo] = bar, L[foo] O(1)
Isso sim é fantástico
meh
Listas Se comportam como RAM porque são modeladas da mesma
forma
Criação Usa 20 bytes de overhead (classe, len, tamanho da
alocação e posição)
mas Uma lista vazia não tem memória alocada para os
dados.
Quando l.append(1)
Acontece a Alocação Aloca espaço para 4 elementos Quando chega
em 4 aloca espaço para 8 e copia os 4 Depois 16
E assim por diante
Ou Quase 25, 35, 46, 58, 72, 88
L.append(foo) é O(1)
Here be monsters
Cons
L.pop(0) é O(n)
L.pop(0) é O(n)-ish
Alternativa from collections import deque deque()
Deques Double Ended QUEue Internamente é uma lista ligada dupla
Prós Filas FIFO e não muito mais
De volta as listas
foo in L é O(n)
Alternativas set()
Dicionários
Dicionários? >>> d = {} >>> d['a'] = 1
Interlúdio >>> hash('a') 12416037344 >>> bin(hash('a') '0b1011100100000011011011000111100000'
Hashtable
Hash 'a' >>> hash('a') 12416037344 >>> bin(hash('a') '...000'
Hashtable
Hash 'b' >>> d['b'] = 2 >>> hash('b') 12544037731 >>>
bin(hash('b') '...011'
Hashtable
Hash 'c' >>> d['c'] = 3 >>> hash('c') 12672038114 >>>
bin(hash('c') '...010'
Hashtable
None
Hash 'j' >>> d['j'] = 4 >>> hash('j') 13568040811 >>>
bin(hash('j') '...011'
Enquanto isso na Hashtable
Outra vez na Hashtable
Duas coisas >>> d1 = {'a':1, 'j':4, 'b':2} >>> d2
= {'a':1, 'b':2, 'j':4} >>> d1 {'a': 1, 'j': 4, 'b': 2} >>> d2 {'a': 1, 'b': 2, 'j': 4} >>> d1 == d2 True
Dicionários não tem ordem Tem sim! A ordem da hashtable
>>> {'a':1, 'j':4, 'b':2}.keys() ['a', 'j', 'b'] >>> {'a':1, 'j':4, 'b':2}.values() [1, 4, 2]
Existem bem mais sutilezas Vídeo da pycon 2010: the might
dictionary
Sets
Sets Implementação igual dos dicionários só que sem o valor.
Então:
foo in s é O(1)
Grafos
A B C D
A B C D
Duas formas a,b,c,d = range(4) n = [[0,1,1,0], [0,0,1,0], [0,0,0,1],
[0,0,0,0]] >>> n[a][b] 1
Ou n = { 'a': set('bc'), 'b': set('c'), 'c': set('d'),
'd': set()} Python Patterns implementing graphs --Guido van Rossum
A B C D 2 3 4 5
passe para dicionários n = { 'a': {'b':2, 'c':3}, 'b':
{'c':4}, 'c': {'d':5}, 'd': {}}
Créditos http://www.flickr.com/photos/autumn_bliss/414160195 http://www.flickr.com/photos/autumn_bliss/414160148
Dúvidas? @fractal +Adriano Petrich [codando.com.br, sfp.adrianopetrich.com, blog.adrianopetrich.com]