In [2]:
eng2sp = dict()
In [3]:
eng2sp['one'] = 'uno'
In [4]:
eng2sp
Out[4]:
In [5]:
len(eng2sp)
Out[5]:
In [14]:
eng2sp[dict()] = 'c'
In [9]:
eng2sp
Out[9]:
In [13]:
di = {'a':'b', eng2sp:'c'}
In [11]:
di
Out[11]:
In [16]:
eng2sp.values()
Out[16]:
In [17]:
def histogram(s):
d = dict()
for c in s:
if c not in d:
d[c] = 1
else:
d[c] += 1
return d
In [18]:
print(histogram('abcbdbfedfbcdbcncbd'))
In [20]:
def word_count(s):
d = dict()
l = s.split(' ')
for w in l:
if w not in d:
d[w] = 1
else:
d[w] += 1
return d
In [21]:
print(word_count('school is school'))
In [22]:
h = histogram('a')
In [23]:
h
Out[23]:
In [28]:
h.get('b',0)
Out[28]:
In [29]:
print(h.get('b'))
In [32]:
def histogram2(s):
d = {}
for c in s:
d[c] = d.get(c,0) + 1
return d
In [33]:
print(histogram2('abbc'))
In [34]:
def print_hist(h):
for c in h:
print(c, h[c])
In [35]:
print_hist(histogram2('aabbcc'))
In [36]:
sorted([3,2,1])
Out[36]:
In [37]:
sorted({'c':1, 'b':2, 'a':3})
Out[37]:
In [38]:
def print_hist2(h):
for c in sorted(h):
print(c, h[c])
In [40]:
print_hist2(histogram2('ccbbaa'))
In [41]:
bool(0)
Out[41]:
In [42]:
bool(1)
Out[42]:
In [43]:
bool([])
Out[43]:
In [45]:
bool([1])
Out[45]:
In [46]:
any([1,2,3])
Out[46]:
In [47]:
any([0,1,2])
Out[47]:
In [48]:
all([0,1,2])
Out[48]:
In [49]:
all([1,2,3])
Out[49]:
In [50]:
all([]) #특이사항
Out[50]:
In [55]:
def reverse_lookup(d, v):
for k in d:
if d[k] == v:
return k
raise LookupError('찾는 키값이 없습니다.')
In [56]:
d = {'a':1, 'b':2, 'c':1}
In [57]:
reverse_lookup(d, 1)
Out[57]:
In [58]:
reverse_lookup(d, 10)
In [59]:
def invert_dict(d):
inverse = {}
for key in d:
val = d[key]
if val not in inverse:
inverse[val] = [key]
else:
inverse[val].append(key)
return inverse
In [60]:
invert_dict(histogram('aabbbbbccdadfadfa'))
Out[60]:
In [61]:
(1,2)
Out[61]:
In [62]:
{(1,2):'a'}
Out[62]:
In [100]:
know = {0:0, 1:0}
def new_fibo(n):
global know
if n in know:
return int(know[n])
know[n] = (new_fibo(n-2) + new_fibo(n-1))
In [101]:
new_fibo(20)
In [72]:
def fibo(n):
if n in [0,1]:
return 1
return fibo(n-1) + fibo(n-2)
In [73]:
fibo(3)
Out[73]:
In [74]:
fibo(5)
Out[74]:
In [102]:
for i in range(10):
print(fibo(i))
In [106]:
%pprint
In [109]:
pprint([1,2,3])
In [110]:
import pprint as pp
In [111]:
pp.pprint("d")
In [112]:
invert_dict(histogram('aabbbbbccdadfadfa'))
Out[112]:
In [113]:
pp.pprint(invert_dict(histogram('aabbbbbccdadfadfa')))
In [114]:
a = { 'result': True, 'records': [ { 'id': 1, 'value': 'First Value' }, { 'id': 2, 'value': 'Second Value' } ], 'count': 2 }
In [115]:
a
Out[115]:
In [116]:
pp.pprint(a)
In [119]:
(1,2) < (3,4)
Out[119]:
In [120]:
(3,4) < (1,2,3,4,5,6)
Out[120]:
In [121]:
[1,2] < [3,4]
Out[121]:
In [122]:
[3,4] < [1,2]
Out[122]:
In [126]:
(1,[3,20,3]) < (1,[3,4,5])
Out[126]:
In [127]:
"a,b,c".split(',')
Out[127]:
In [128]:
t = (7,3)
In [129]:
divmod(t)
In [130]:
divmod(*t)
Out[130]:
In [131]:
def sumall(*args):
return sum(list(args))
In [132]:
sumall(1,2,3)
Out[132]:
In [133]:
sumall(4,5,6,7)
Out[133]:
In [134]:
sum(1,2,3)
In [135]:
sumall(*t)
Out[135]:
In [152]:
l1 = [1,2,3]
l2 = [4,5,6]
l3 = [7,8,9]
In [153]:
list(zip(l1,l2,l3))
Out[153]:
In [154]:
d = {'a':0,'b':1}
In [155]:
for key, value in d.items():
print(key, value)
In [158]:
dict(zip('abc', range(3)))
Out[158]:
In [162]:
from structshape import structshape
In [163]:
's--s'.split('-')
Out[163]:
In [164]:
's-'.split('-')
Out[164]:
In [168]:
%%javascript
var a = [1,2,3,4,5]
a.length
In [ ]: