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
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
PostgreSQLのRow Level SecurityをPHPのORMで扱う Eloquent vs Doctrine #phpcon #track2
77web
2
530
AI駆動のマルチエージェントによる業務フロー自動化の設計と実践
h_okkah
0
170
NPOでのDevinの活用
codeforeveryone
0
850
Rails Frontend Evolution: It Was a Setup All Along
skryukov
0
160
PipeCDのプラグイン化で目指すところ
warashi
1
280
Azure AI Foundryではじめてのマルチエージェントワークフロー
seosoft
0
180
プロダクト志向ってなんなんだろうね
righttouch
PRO
0
190
XP, Testing and ninja testing
m_seki
3
250
生成AI時代のコンポーネントライブラリの作り方
touyou
1
230
今ならAmazon ECSのサービス間通信をどう選ぶか / Selection of ECS Interservice Communication 2025
tkikuc
21
4k
AIと”コードの評価関数”を共有する / Share the "code evaluation function" with AI
euglena1215
1
170
おやつのお供はお決まりですか?@WWDC25 Recap -Japan-\(region).swift
shingangan
0
140
Featured
See All Featured
Imperfection Machines: The Place of Print at Facebook
scottboms
267
13k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
26k
Rebuilding a faster, lazier Slack
samanthasiow
83
9.1k
Building a Modern Day E-commerce SEO Strategy
aleyda
42
7.4k
Build your cross-platform service in a week with App Engine
jlugia
231
18k
Designing Experiences People Love
moore
142
24k
Scaling GitHub
holman
460
140k
We Have a Design System, Now What?
morganepeng
53
7.7k
Bootstrapping a Software Product
garrettdimon
PRO
307
110k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
50
5.5k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
Stop Working from a Prison Cell
hatefulcrawdad
271
21k
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]