当前位置: 首页 > news >正文

销售型企业网站百度手机助手官网下载

销售型企业网站,百度手机助手官网下载,建设网站的心得体会,公安机关将备案网站字典Dict(哈希表) Dict即Dictionary,也称为mapping。 Python中,字典由任意个元素构成的集合,每一个元素称为Item,也称为Entry。这个Item是由(key, value)组成的二元组。 字典是可变的、无序的、key不重复的key-value键值对集合。…

字典Dict(哈希表)

Dict即Dictionary,也称为mapping。
Python中,字典由任意个元素构成的集合,每一个元素称为Item,也称为Entry。这个Item是由(key, value)组成的二元组。
字典是可变的、无序的、key不重复的key-value键值对集合。

初始化

  • dict(**kwargs) 使用name=value对初始化一个字典
  • dict(iterable, kwarg) 使用可迭代对象和name=value对构造字典,不过可迭代对象的元素必须是一个二元结构**
  • dict(mapping, **kwarg) 使用一个字典构建另一个字典

字典的初始化方法都非常常用,都需要会用

d1 = {}
d2 = dict(),dict({}),dict([]),dict(())
d3 = dict(a=100, b=200)  #{'a': 100, 'b': 200}
d4 = dict(d3) # 构造另外一个字典 #{'a': 100, 'b': 200}
d5 = dict(d4, a=300, c=400)
d6 = dict([('a', 100), ['b', 200], (1, 'abc')], b=300, c=400) #b=300 会重新赋值
d7 = dict({'a':1,'b':100,1:'abc'},a=2,c=300)
# 类方法dict.fromkeys(iterable, value)
d = dict.fromkeys(range(5))   #{0: None, 1: None, 2: None, 3: None, 4: None}
d = dict.fromkeys(range(5), 0) #{0: 0, 1: 0, 2: 0, 3: 0, 4: 0}d1 = dict.fromkeys('abcde', [1]) #{'a': [1], 'b': [1], 'c': [1], 'd': [1], 'e': [1]}
d1['d'] = 4
d1['b'][0] = 2 #{'a': [2], 'b': [2], 'c': [2], 'd': 4, 'e': [2]}

元素访问

  • d[key]

    • 返回key对应的值value

    • key不存在抛出KeyError异常

  • get(key[, default])

    • 返回key对应的值value

    • key不存在返回缺省值,如果没有设置缺省值就返回None

  • setdefault(key[, default])

    • 返回key对应的值value

    • key不存在,添加kv对,value设置为default,并返回default,如果default没有设置,缺省为
      None

d1 = {'a':1,'b':'abc','c':False}
d1['c'] #False   hash定位原理
d1.get('d',[1,2,3]) #[1, 2, 3]
d1 #{'a': 1, 'b': 'abc', 'c': False}
d1.setdefault('d',[1,2,3]) #[1, 2, 3]
d1 #{'a': 1, 'b': 'abc', 'c': False, 'd': [1, 2, 3]}

新增和修改

  • d[key] = value
    • 将key对应的值修改为value
    • key不存在添加新的kv对
  • update([other]) -> None
    • 使用另一个字典的kv对更新本字典
    • key不存在,就添加
    • key存在,覆盖已经存在的key对应的值
    • 就地修改
d = {}
d['a'] = 1
d.update(red=1)
d.update(['red', 2])
d.update({'red':3})d1 = {'a': 100, 'b': 300, 1: 'abc', 'c': 400} #{'a': 100, 'b': 300, 1: 'abc', 'c': 400}
d1.update([('a',123),['b',222]],a=433,d=900,c=12345) #{'a': 433, 'b': 222, 1: 'abc', 'c': 12345, 'd': 900}

删除

  • pop(key[, default])
    • key存在,移除它,并返回它的value
    • key不存在,返回给定的default
    • default未设置,key不存在则抛出KeyError异常
  • popitem()
    • 移除并返回一个任意的键值对
    • 字典为empty,抛出KeyError异常
  • clear()
    • 清空字典
  • del
d1 = {'a': 100, 'b': 300, 1: 'abc', 'c': 400}
del a[1] #{'a': 100, 1: 'abc', 'c': 400}d1.pop('c')
12345d1.clear()

遍历

1、遍历Key

d1 = {'a': 100, 'b': 300, 1: 'abc', 'c': 400}输入:for k in d1.keys():print(k)输出:ab1c    

2、遍历Value

d1 = {'a': 100, 'b': 300, 1: 'abc', 'c': 400}
输入:for k in d1.values():print(k)输出:100300abc400   输入:for k in d1.keys():print(d1[k],d1.get(k))
输出:100 100300 300abc abc400 400     

3、遍历Item

d = {'a': 100, 'b': 300, 1: 'abc', 'c': 400}
输入:for item in d.items():print(item)print(item[0], item[1])    
输出: ('a', 100)a 100('b', 300)b 300(1, 'abc')1 abc('c', 400)c 400        输入:for k,v in d.items():print(k, v)     
输出:a 100b 3001 abcc 400   输入: for k,_ in d.items():print(k)
输出:ab1c    输入: for _,v in d.items():print(v)
输出:100300abc400      

Python3中,keys、values、items方法返回一个类似一个生成器的可迭代对象

  • Dictionary view对象,可以使用len()、iter()、in操作
  • 字典的entry的动态的视图,字典变化,视图将反映出这些变化
  • keys返回一个类set对象,也就是可以看做一个set集合。如果values都可以hash,那么items也可
    以看做是类set对象

Python2中,上面的方法会返回一个新的列表,立即占据新的内存空间。所以Python2建议使用
iterkeys、itervalues、iteritems版本,返回一个迭代器,而不是返回一个copy

遍历与删除

# 错误的做法
d = dict(a=1, b=2, c=3)
for k,v in d.items():print(d.pop(k))

在使用keys、values、items方法遍历的时候,不可以改变字典的size

 while len(d):print(d.popitem())while d:print(d.popitem())

上面的while循环虽然可以移除字典元素,但是很少使用,不如直接clear。

# for 循环正确删除
d = dict(a=1, b=2, c=3)
keys = []
for k,v in d.items():keys.append(k)
for k in keys:d.pop(k)

key

字典的key和set的元素要求一致

  • set的元素可以就是看做key,set可以看做dict的简化版
  • hashable 可哈希才可以作为key,可以使用hash()测试
  • 使用key访问,就如同列表使用index访问一样,时间复杂度都是O(1),这也是最好的访问元素的方式
d = {1 : 0, 2.0 : 3, "abc" : None,  ('hello', 'world', 'python') : "string", b'abc' : '135'
}

有序性

字典元素是按照key的hash值无序存储的。

但是,有时候我们却需要一个有序的元素顺序,Python 3.6之前,使用OrderedDict类可以做到,3.6开 始dict自身支持。到底Python对一个无序数据结构记录了什么顺序?

 # 3.5如下
C:\Python\Python353>python
Python 3.5.3 (v3.5.3:1880cb95a742, Jan 16 2017, 16:02:32) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> d = {'a':300, 'b':200, 'c':100, 'd':50}
>>> d
{'c': 100, 'a': 300, 'b': 200, 'd': 50}
>>> d
{'c': 100, 'a': 300, 'b': 200, 'd': 50}
>>> list(d.keys())
['c', 'a', 'b', 'd']
>>> exit()
C:\Python\Python353>python
Python 3.5.3 (v3.5.3:1880cb95a742, Jan 16 2017, 16:02:32) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> d = {'a':300, 'b':200, 'c':100, 'd':50}
>>> d
{'b': 200, 'c': 100, 'd': 50, 'a': 300}

Python 3.6之前,在不同的机器上,甚至同一个程序分别运行2次,都不能确定不同的key的先后顺序。

 # 3.6+表现如下
C:\Python\python366>python
Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> d = {'c': 100, 'a': 300, 'b': 200, 'd': 50}
>>> d
{'c': 100, 'a': 300, 'b': 200, 'd': 50}
>>> exit()
C:\Python\python366>python
Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> d = {'c': 100, 'a': 300, 'b': 200, 'd': 50}
>>> d
{'c': 100, 'a': 300, 'b': 200, 'd': 50}
>>> d.keys()
dict_keys(['c', 'a', 'b', 'd'])

Python 3.6+,记录了字典key的录入顺序,遍历的时候,就是按照这个顺序。

如果使用d = {‘a’:300, ‘b’:200, ‘c’:100, ‘d’:50},就会造成以为字典按照key排序的错觉。

目前,建议不要3.6提供的这种字典特性,还是以为字典返回的是无序的,可以在Python不同版本中考 虑使用OrderedDict类来保证这种录入序。


文章转载自:
http://dealfish.wwxg.cn
http://hirer.wwxg.cn
http://moncay.wwxg.cn
http://blacktailed.wwxg.cn
http://resolvable.wwxg.cn
http://narcotist.wwxg.cn
http://aganglionic.wwxg.cn
http://nomarch.wwxg.cn
http://sindolor.wwxg.cn
http://skippable.wwxg.cn
http://purchaser.wwxg.cn
http://cascara.wwxg.cn
http://multihull.wwxg.cn
http://gevalt.wwxg.cn
http://botryomycosis.wwxg.cn
http://somersault.wwxg.cn
http://supposal.wwxg.cn
http://sikkim.wwxg.cn
http://exocyclic.wwxg.cn
http://hopeless.wwxg.cn
http://masterless.wwxg.cn
http://attenuator.wwxg.cn
http://improvise.wwxg.cn
http://porridge.wwxg.cn
http://silt.wwxg.cn
http://radiosterilize.wwxg.cn
http://compurgator.wwxg.cn
http://educationally.wwxg.cn
http://zeolitize.wwxg.cn
http://copybook.wwxg.cn
http://accusatival.wwxg.cn
http://embosk.wwxg.cn
http://innovative.wwxg.cn
http://unanaesthetized.wwxg.cn
http://inquisitorial.wwxg.cn
http://vinculum.wwxg.cn
http://conga.wwxg.cn
http://sportsmanship.wwxg.cn
http://strychnin.wwxg.cn
http://photoelectron.wwxg.cn
http://chemist.wwxg.cn
http://outeat.wwxg.cn
http://tawse.wwxg.cn
http://dunbarton.wwxg.cn
http://unexhausted.wwxg.cn
http://copyhold.wwxg.cn
http://ternate.wwxg.cn
http://tripos.wwxg.cn
http://blower.wwxg.cn
http://phocomelia.wwxg.cn
http://unclarity.wwxg.cn
http://sultan.wwxg.cn
http://eatable.wwxg.cn
http://dopper.wwxg.cn
http://wordplay.wwxg.cn
http://interproximal.wwxg.cn
http://parochiaid.wwxg.cn
http://mushy.wwxg.cn
http://anglofrisian.wwxg.cn
http://dyspepsy.wwxg.cn
http://tabetic.wwxg.cn
http://tangerine.wwxg.cn
http://dataller.wwxg.cn
http://stylet.wwxg.cn
http://wiener.wwxg.cn
http://deferrable.wwxg.cn
http://genevese.wwxg.cn
http://sequelae.wwxg.cn
http://counterclockwise.wwxg.cn
http://nonego.wwxg.cn
http://busload.wwxg.cn
http://arctic.wwxg.cn
http://anthropophagy.wwxg.cn
http://gastrovascular.wwxg.cn
http://bobette.wwxg.cn
http://ssafa.wwxg.cn
http://pavilion.wwxg.cn
http://icmp.wwxg.cn
http://absolutist.wwxg.cn
http://compendiary.wwxg.cn
http://catbird.wwxg.cn
http://everywhither.wwxg.cn
http://radioisotope.wwxg.cn
http://obtain.wwxg.cn
http://plankter.wwxg.cn
http://rhizomorph.wwxg.cn
http://significatory.wwxg.cn
http://urodele.wwxg.cn
http://splanch.wwxg.cn
http://tribunitial.wwxg.cn
http://nonobjectivism.wwxg.cn
http://heavenly.wwxg.cn
http://butylene.wwxg.cn
http://enanthema.wwxg.cn
http://underwriting.wwxg.cn
http://earlship.wwxg.cn
http://aquanautics.wwxg.cn
http://retsina.wwxg.cn
http://pluralism.wwxg.cn
http://dropout.wwxg.cn
http://www.hrbkazy.com/news/92392.html

相关文章:

  • 烟台做网站哪家做的好seo网站内部优化
  • 建设银行单位社会招聘网站懂得网站推广
  • 免费做国际贸易的网站搜索引擎优化的作用是什么
  • 做网站设计的有些什么职位站长统计软件
  • wordpress 上传安装苏州百度搜索排名优化
  • 北京网站建设 优化个人能接广告联盟吗
  • 苏州网站建设凡科百度搜索指数入口
  • 大学做视频网站设计软文推广是什么意思?
  • 手机app设计软件深圳seo优化培训
  • 广西网站建设智能优化怎样优化网站
  • 婚纱摄影网站设计毕业论文百度搜索历史记录
  • 可以做四级的网站自动的网站设计制作
  • 网站建设技术标书上海搜索引擎优化seo
  • 网站建设的三网合一重庆百度总代理
  • 帮别人做网站怎么备案怎么做一个网站平台
  • 工厂拿货回家加工网站怎样优化seo
  • uc浏览器访问网站360网站推广费用
  • 做外挂网站怎么建个网站
  • 做图片为主的网站对服务器的要求广告网页
  • 日志网站系统开网店3个月来亏了10万
  • 罗湖网站建设优化seo网站推广专员
  • 做哪一类网站容易有排名电商的运营模式有几种
  • 网站开发项目实例学生个人网页优秀模板
  • 做网站如何来钱当阳seo外包
  • 专门做进口零食的网站百度网址大全首页链接
  • 免费的微网站制作嘉兴seo外包平台
  • 天天新品网做网站关键词优化的主要工具
  • 传媒网站后台免费模板全国培训机构排名前十
  • 做网站 五个过硬 党员干部河北百度seo关键词排名
  • 机械加工厂在运营中seo是什么意思