Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0f80870

Browse files
committed
comprehnesions, decorators
1 parent0e15359 commit0f80870

File tree

4 files changed

+105
-0
lines changed

4 files changed

+105
-0
lines changed

‎python_algorithms/collections/collections.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,16 @@
77
# counter
88
# ChainMap
99
# deque
10+
11+
12+
# sequence types:
13+
# tuple, list, string, range
14+
15+
# unpacking sequences
16+
p='ray',0x30,'python'
17+
18+
# no:
19+
name=p[0]
20+
hex=p[1]
21+
# yes:
22+
name,hex=p

‎python_algorithms/collections/dictionaries/dict.py‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,54 @@
1+
# Construct a dictionary from pairs
2+
3+
# izip returns iterables
4+
# xrange is better than range
5+
# Loop over dict keys and values
6+
7+
# creates many items in mem
8+
fork,vind.items()
9+
10+
# better in space
11+
fork,vind.iteritems()
12+
13+
names= []
14+
colors= []
15+
# faster than zip
16+
d=dict(izip(names,colors))
17+
18+
# counting with dicts
19+
# ok:
20+
d= {}
21+
forcolorincolors
22+
ifcolornotind:
23+
d[color]=0
24+
d[color]+=1
25+
26+
# best:
27+
forcolorincolors:
28+
d[color]=d.get(color,0)+1
29+
30+
# grouping with dicts
31+
# ok:
32+
names= ['fred','jim']
33+
d= {}
34+
fornameinnames:
35+
key=len(name)
36+
ifkeynotind:
37+
d[key]= []
38+
d[key].append(name)
39+
40+
# better:
41+
d= {}
42+
fornameinnames:
43+
key=len(name)
44+
d.setdefault(key, []).append(name)
45+
46+
# most best:
47+
d=defaultdict(list)
48+
fornameinnames:
49+
key=len(name)
50+
d[key].append(name)
51+
152
# to convert a list of tuples to a dictionary using the first value as the key.
253
# Looked like this (making a database of comic book files):
354

‎python_algorithms/decorators.py‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# using decorators to factor-out administrative logic
2+
defweb_lookup(url,saved={}):
3+
ifurlinsaved:
4+
returnsaved[url]
5+
page=urllib.urlopen(url).read()
6+
saved[url]=page
7+
returnpage
8+
9+
# better:
10+
@cache# new one is lrucache
11+
defweblookup(url):
12+
returnurllib.urlopen(url).read()
13+
14+
# caching decorator - your utils libraries should be full of these
15+
defcache(func):
16+
saved= {}
17+
@wraps(func)
18+
defnewfunc(*args):
19+
ifargsinsaved:
20+
returnnewfunc(*args)
21+
result=func(*args)
22+
saved[args]=result
23+
returnresult
24+
returnnewfunc
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
matrix= [[1,2,3], [4,5,6]]
2+
3+
# for loop
4+
negative_matrix= []
5+
forrowinmatrix:
6+
negative_matrix.append([-nforninrow])
7+
8+
# list comprehension
9+
negative_evens_matrix= [
10+
[-nforninrow]# or negate(n)
11+
forrowinmatrix
12+
ifn%2==0
13+
]
14+
15+
fromitertoolsimportizip
16+
list(zip(*matrix))# returns tuples
17+
print([list(col)forcolinizip(*matrix)])

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp