可以从任何以键值对作为元素的可迭代对象中构建出字典。
一般可以将元组的列表利用推导变成字典的数据类型。coding如下
country_code = {country:code for code,country in dial_codes}
dial_codes = [ (86, 'China'),
(91, 'India'),
(1, 'United States'),
(62, 'Indonesia'),
(55, 'Brazil'),
(92, 'Pakistan'),
(880, 'Bangladesh'),
(234, 'Nigeria'),
(7, 'Russia'),
(81, 'Japan') ]
print(dial_codes)
# 利用推导来获取数据
country_code = {country:code for code, country in dial_codes}
print(country_code)
# 这里
country_code2 = {code:country.upper() for code,country in dial_codes}
print(country_code2)
[(86, 'China'), (91, 'India'), (1, 'United States'), (62, 'Indonesia'), (55, 'Brazil'), (92, 'Pakistan'), (880, 'Bangladesh'), (234, 'Nigeria'), (7, 'Russia'), (81, 'Japan')]
{'Brazil': 55, 'Bangladesh': 880, 'Russia': 7, 'China': 86, 'Nigeria': 234, 'Japan': 81, 'Indonesia': 62, 'United States': 1, 'India': 91, 'Pakistan': 92}
{880: 'BANGLADESH', 1: 'UNITED STATES', 86: 'CHINA', 55: 'BRAZIL', 7: 'RUSSIA', 234: 'NIGERIA', 91: 'INDIA', 92: 'PAKISTAN', 62: 'INDONESIA', 81: 'JAPAN'}
dict、defaultdict 和 OrderedDict 的常见方法,后面两个数据类型是 dict 的变种,如下:
例如 update 方法:
一般处理字典中找不到键的方法:
这里推荐的方法:
occurrences = index.get(word,[])
或者if key not in my_dict
。# 使用d.get(k,default)处理不存在的键
def showWord(filePath):
import sys
import re
# 正则表达式
WORD_RE = re.compile(r'\w+')
index = {}
with open(filePath,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)
# 查找key否存在,不存在则返回一个空列表[]
occurrences = index.get(word,[])
# 将找打的新的数据放到列表的后面
occurrences.append(location)
# 再把列表放到对应的字典中
index[word] = occurrences
# 利用关键字key=参数来排序,
# 意思是在排序前,先将key变成大写字母
for word in sorted(index,key=str.upper):
print(word,index[word])
showWord("waliai.txt")
about [(2, 36)]
accounts [(1, 29), (2, 92), (3, 36)]
AI [(1, 10), (2, 46), (3, 24)]
and [(2, 61)]
are [(2, 32), (4, 4)]
can [(2, 5)]
find [(2, 9)]
follow [(3, 8)]
for [(4, 16)]
in [(1, 38), (2, 76)]
information [(2, 49)]
is [(1, 13)]
offical [(2, 84)]
official [(1, 20), (3, 27)]
papers [(2, 19)]
please [(3, 1)]
some [(2, 14)]
techenical [(2, 65)]
The [(1, 1)]
the [(1, 16), (2, 42), (3, 15)]
this [(2, 79)]
waiting [(4, 8)]
Wali [(1, 5), (3, 19)]
We [(4, 1)]
Wechat [(1, 41)]
which [(2, 26)]
You [(2, 1)]
you [(4, 20)]
# 使用setdefault处理不存在的键
def showWordBysetdefault(filePath):
import sys
import re
# 正则表达式
WORD_RE = re.compile(r'\w+')
index = {}
with open(filePath,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)
#使用setdefault 函数来出来
# 如果单词不存在,则把单词和空列表放进映射,返回一个空列表
index.setdefault(word,[]).append(location)
# 利用关键字key=参数来排序,
# 意思是在排序前,先将key变成大写字母
for word in sorted(index,key=str.upper):
print(word,index[word])
showWordBysetdefault("waliai.txt")
about [(2, 36)]
accounts [(1, 29), (2, 92), (3, 36)]
AI [(1, 10), (2, 46), (3, 24)]
and [(2, 61)]
are [(2, 32), (4, 4)]
can [(2, 5)]
find [(2, 9)]
follow [(3, 8)]
for [(4, 16)]
in [(1, 38), (2, 76)]
information [(2, 49)]
is [(1, 13)]
offical [(2, 84)]
official [(1, 20), (3, 27)]
papers [(2, 19)]
please [(3, 1)]
some [(2, 14)]
techenical [(2, 65)]
The [(1, 1)]
the [(1, 16), (2, 42), (3, 15)]
this [(2, 79)]
waiting [(4, 8)]
Wali [(1, 5), (3, 19)]
We [(4, 1)]
Wechat [(1, 41)]
which [(2, 26)]
You [(2, 1)]
you [(4, 20)]
这里要处理一个问题是:有时候为了方便起见,就算某个键在映射里不存在,我们也希望在通过这个键读取值的时候能得到一个默认值。这里两个方法:
因为在定义defaultdict对象的时候会提供一个构造方法,该方法可以处理不存在的键,具体是通过__getitem__
返回某种默认的值。这个值跟上面setdefault很像。
dd = defaultdict(list),如果dd中不存在 key,会如此处理:
# 使用defaultdict处理不存在的键
def showWordBydefaultdict(filePath):
import sys
import re
import collections
# 正则表达式
WORD_RE = re.compile(r'\w+')
index = collections.defaultdict(list)
with open(filePath,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)
# 利用关键字key=参数来排序,
# 意思是在排序前,先将key变成大写字母
for word in sorted(index,key=str.upper):
print(word,index[word])
showWordBydefaultdict("waliai.txt")
about [(2, 36)]
accounts [(1, 29), (2, 92), (3, 36)]
AI [(1, 10), (2, 46), (3, 24)]
and [(2, 61)]
are [(2, 32), (4, 4)]
can [(2, 5)]
find [(2, 9)]
follow [(3, 8)]
for [(4, 16)]
in [(1, 38), (2, 76)]
information [(2, 49)]
is [(1, 13)]
offical [(2, 84)]
official [(1, 20), (3, 27)]
papers [(2, 19)]
please [(3, 1)]
some [(2, 14)]
techenical [(2, 65)]
The [(1, 1)]
the [(1, 16), (2, 42), (3, 15)]
this [(2, 79)]
waiting [(4, 8)]
Wali [(1, 5), (3, 19)]
We [(4, 1)]
Wechat [(1, 41)]
which [(2, 26)]
You [(2, 1)]
you [(4, 20)]
从字面来理解就很容易理解,这个函数是用来处理miss掉键的。而且这里我们要知道的是:
所有的映射类型在处理找不到的键的时候,都会牵扯到 missing 方法
dict 基类并没有实现该方法,但是如果自己写的类继承dict,还是可以通过实现该方法来处理不存在键。
需要了解的是:__missing__方法只会被__getitem__调用,也就是d[k]这样的格式。
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()
d = StrkeyDict0([('2','tow'),('4','four')])
print (d['2'])
print (d[4])
#print(d[3])
print(d.get(1,"test"))
tow
four
test
country_code = {country:code for code,country in dial_codes}
index.setdefault(word,[]).append(location)
index = collections.defaultdict(list)
__missing__
分享关于人工智能,机器学习,深度学习以及计算机视觉的好文章,同时自己对于这个领域学习心得笔记。想要一起深入学习人工智能的小伙伴一起结伴学习吧!扫码上车!