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
2
100
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
Tweet
Share
Other Decks in Programming
See All in Programming
MDN Web Docs に日本語翻訳でコントリビュート
ohmori_yusuke
0
650
humanlayerのブログから学ぶ、良いCLAUDE.mdの書き方
tsukamoto1783
0
190
AI Agent の開発と運用を支える Durable Execution #AgentsInProd
izumin5210
7
2.3k
AIと一緒にレガシーに向き合ってみた
nyafunta9858
0
230
CSC307 Lecture 02
javiergs
PRO
1
780
AI時代のキャリアプラン「技術の引力」からの脱出と「問い」へのいざない / tech-gravity
minodriven
21
7.2k
今こそ知るべき耐量子計算機暗号(PQC)入門 / PQC: What You Need to Know Now
mackey0225
3
380
AI時代の認知負荷との向き合い方
optfit
0
160
並行開発のためのコードレビュー
miyukiw
0
130
余白を設計しフロントエンド開発を 加速させる
tsukuha
7
2.1k
副作用をどこに置くか問題:オブジェクト指向で整理する設計判断ツリー
koxya
1
610
20260127_試行錯誤の結晶を1冊に。著者が解説 先輩データサイエンティストからの指南書 / author's_commentary_ds_instructions_guide
nash_efp
1
970
Featured
See All Featured
How Fast Is Fast Enough? [PerfNow 2025]
tammyeverts
3
450
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
27k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
133
19k
Designing for Performance
lara
610
70k
Conquering PDFs: document understanding beyond plain text
inesmontani
PRO
4
2.3k
The Impact of AI in SEO - AI Overviews June 2024 Edition
aleyda
5
730
Odyssey Design
rkendrick25
PRO
1
490
Leveraging LLMs for student feedback in introductory data science courses - posit::conf(2025)
minecr
0
140
The Invisible Side of Design
smashingmag
302
51k
How Software Deployment tools have changed in the past 20 years
geshan
0
32k
Building AI with AI
inesmontani
PRO
1
690
Six Lessons from altMBA
skipperchong
29
4.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]