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
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
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
Fragmented Architectures
denyspoltorak
0
160
izumin5210のプロポーザルのネタ探し #tskaigi_msup
izumin5210
1
130
AWS re:Invent 2025参加 直前 Seattle-Tacoma Airport(SEA)におけるハードウェア紛失インシデントLT
tetutetu214
2
110
16年目のピクシブ百科事典を支える最新の技術基盤 / The Modern Tech Stack Powering Pixiv Encyclopedia in its 16th Year
ahuglajbclajep
5
1k
「ブロックテーマでは再現できない」は本当か?
inc2734
0
1k
OCaml 5でモダンな並列プログラミングを Enjoyしよう!
haochenx
0
140
HTTPプロトコル正しく理解していますか? 〜かわいい猫と共に学ぼう。ฅ^•ω•^ฅ ニャ〜
hekuchan
2
690
AIエージェントのキホンから学ぶ「エージェンティックコーディング」実践入門
masahiro_nishimi
5
470
AtCoder Conference 2025
shindannin
0
1.1k
Oxlint JS plugins
kazupon
1
970
MDN Web Docs に日本語翻訳でコントリビュート
ohmori_yusuke
0
650
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
580
Featured
See All Featured
Ecommerce SEO: The Keys for Success Now & Beyond - #SERPConf2024
aleyda
1
1.8k
How To Stay Up To Date on Web Technology
chriscoyier
791
250k
The untapped power of vector embeddings
frankvandijk
1
1.6k
SEO in 2025: How to Prepare for the Future of Search
ipullrank
3
3.3k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
231
22k
Design in an AI World
tapps
0
140
Mozcon NYC 2025: Stop Losing SEO Traffic
samtorres
0
140
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
254
22k
Tell your own story through comics
letsgokoyo
1
810
Leadership Guide Workshop - DevTernity 2021
reverentgeek
1
200
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
17k
Building an army of robots
kneath
306
46k
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]