In [5]:
from collections import abc
my_dict = {}
In [6]:
isinstance(my_dict, abc.Mapping)
Out[6]:
In [7]:
tt = (1, 2, (30, 40))
hash(tt)
Out[7]:
In [8]:
tl = (1, 2, [30, 40])
hash(tl)
In [9]:
tf = (1, 2, frozenset([30, 40]))
hash(tf)
Out[9]:
In [12]:
a = dict(one=1, two=2, three=3)
b = {'one': 1, 'two': 2, 'three': 3}
c = dict(zip(['one', 'two', 'three'],[1, 2, 3]))
d = dict([('two', 2), ('one', 1), ('three', 3)])
e = dict({'three': 3, 'one': 1, 'two': 2})
In [13]:
a == b == c == d == e
Out[13]:
In [22]:
DIAL_CODES = [
(86, 'China'),
(91, 'India'),
(1, 'United States'),
(62, 'Indonesia'),
(55, 'Brazil'),
(92, 'Pakistan'),
(880, 'Bangladesh'),
(234, 'Nigeria'),
(7, 'Russia'),
(81, 'Japan'),
]
In [23]:
country_code = {country: code for code, country in DIAL_CODES}
In [24]:
country_code
Out[24]:
In [26]:
{code: country.upper() for country, code in country_code.items() if code < 66}
Out[26]:
In [34]:
a.values()
Out[34]:
In [35]:
import sys
import re
In [36]:
WORD_RE = re.compile(r'\w+')
In [40]:
index = {}
with open('zen.txt', encoding='utf-8') as fp:
for line_no, line in enumerate(fp, 1):
for match in WORD_RE.finditer(line):
word = match.group()
column_no = match.start()+1
location = (line_no, column_no)
index.setdefault(word, []).append(location)
# occurences = index.get(word, [])
# occurences.append(location)
# index[word] = occurences
In [47]:
# for word in sorted(index, key=str.upper):
# print(word, index[word])
In [51]:
import sys
import re
import collections
In [52]:
WORD_RE = re.compile(r'\w+')
In [53]:
index = collections.defaultdict(list)
with open('zen.txt', encoding='utf-8') as fp:
for line_no, line in enumerate(fp, 1):
for match in WORD_RE.finditer(line):
word = match.group()
column_no = match.start()+1
location = (line_no, column_no)
index[word].append(location)
In [55]:
# for word in sorted(index, key=str.upper):
# print(word, index[word])
In [70]:
class StrKeyDict0(dict):
def __missing__(self, key):
if isinstance(key, str):
raise KeyError(key)
return self[str(key)]
def get(self, key, default=None):
try:
return self[key]
except KeyError:
return default
def __contains__(self, key):
return key in self.keys() or str(key) in self.keys()
In [72]:
# Tests for item retrieval using `d[key]` notation
d = StrKeyDict0([('2', 'two'), ('4', 'four')])
In [62]:
d['2']
Out[62]:
In [63]:
d[4]
Out[63]:
In [64]:
d[1]
In [65]:
d.get('2')
Out[65]:
In [66]:
d.get(4)
Out[66]:
In [67]:
d.get(1, 'N/A')
Out[67]:
In [74]:
2 in d
Out[74]:
In [75]:
1 in d
Out[75]:
In [7]:
import builtins
from collections import ChainMap
import collections
In [5]:
pylookup = ChainMap(locals(), globals(), vars(builtins))
In [8]:
ct = collections.Counter('abracadabra')
ct
Out[8]:
In [9]:
ct.update('aaaaazzz')
In [11]:
ct
Out[11]:
In [12]:
ct.most_common(2)
Out[12]:
In [13]:
import collections
class StrKeyDict(collections.UserDict):
def __missing(self, key):
if isinstance(key, str):
raise KeyError(key)
return self[str(key)]
def __contains__(self, key):
return str(key) in self.data
def __setitem__(self, key, item):
self.data[str(key)] = item
In [14]:
from types import MappingProxyType
In [15]:
d = {1: 'A'}
d_proxy = MappingProxyType(d)
d_proxy
Out[15]:
In [16]:
d_proxy[1]
Out[16]:
In [17]:
d_proxy[2] = 'x'
In [18]:
d[2] = 'B'
In [19]:
d_proxy
Out[19]:
In [20]:
d_proxy[2]
Out[20]:
In [21]:
l = ['spam', 'spam', 'eggs', 'spam']
set(l)
Out[21]:
In [22]:
list(set(l))
Out[22]:
In [24]:
haystack = set([1, 4, 5, 6, 8, 12, 15, 20, 21, 23, 26, 29, 30])
needles = set([0, 1, 2, 5, 8, 10, 22, 23, 29, 30, 31])
In [27]:
found = len(needles & haystack)
found
Out[27]:
In [29]:
found = 0
for n in needles:
if n in haystack:
found +=1
found
Out[29]:
In [30]:
found = len(needles.intersection(haystack))
found
Out[30]:
In [35]:
s = {1}
type(s)
Out[35]:
In [36]:
s
Out[36]:
In [37]:
s.pop()
Out[37]:
In [38]:
s
Out[38]:
In [39]:
from dis import dis
dis('{1}')
In [40]:
dis('set([1])')
In [41]:
frozenset(range(10))
Out[41]:
In [57]:
from unicodedata import name
{chr(i) for i in range(32,256) if 'SIGN' in name(chr(i),'')}
Out[57]:
In [55]:
name(chr(172),'')
Out[55]:
In [58]:
DIAL_CODES = [
(86, 'China'),
(91, 'India'),
(1, 'United States'),
(62, 'Indonesia'),
(55, 'Brazil'),
(92, 'Pakistan'),
(880, 'Bangladesh'),
(234, 'Nigeria'),
(7, 'Russia'),
(81, 'Japan'),
]
In [60]:
d1 = dict(DIAL_CODES)
print('d1:', d1.keys())
In [61]:
d2 = dict(sorted(DIAL_CODES))
print('d2:', d2.keys())
In [62]:
d3 = dict(sorted(DIAL_CODES, key=lambda x:x[1]))
print('d3:', d3.keys())
In [63]:
assert d1 == d2 and d2 == d3
In [ ]:
In [ ]: