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

网站使用网络图片做素材 侵权吗百度快照功能

网站使用网络图片做素材 侵权吗,百度快照功能,安徽省建设行业质量与安全协会网站,赛门仕博做网站怎么样文章目录 序言1. 字典的创建和访问2. 字典如何添加元素3. 字典作为函数参数4. 字典排序 序言 总结字典的一些常见用法 1. 字典的创建和访问 字典是一种可变容器类型,可以存储任意类型对象 key : value,其中value可以是任何数据类型,key必须…

文章目录

      • 序言
      • 1. 字典的创建和访问
      • 2. 字典如何添加元素
      • 3. 字典作为函数参数
      • 4. 字典排序

序言

  • 总结字典的一些常见用法

1. 字典的创建和访问

  • 字典是一种可变容器类型,可以存储任意类型对象

  • key : value,其中value可以是任何数据类型,key必须是不可变的如字符串、数字、元组,不可以用列表

  • key是唯一的,如果出现两次,后一个值会被记住

  • 字典的创建

    • 创建空字典

      dictionary = {}
      
    • 直接赋值创建字典

      dictionary = {'name' : 'Nick', 'age' : 20, 'height' : 175}
      
    • 通过关键字dict和关键字参数创建字典

      dictionary = dict(name='Nick', age=20, height=175)
      
      dictionary = dict()
      for i in range(1, 5):dictionary[i] = i * i
      print(dictionary)		# 输出结果:{1: 1, 2: 4, 3: 9, 4: 16}
      
    • 通过关键字dict和二元组列表创建

      my_list = [('name', 'Nick'), ('age', 20), ('height', 175)]
      dictionary = dict(my_list)
      
    • 通过关键字dict和zip创建

      dictionary = dict(zip('abc', [1, 2, 3]))
      print(dictionary)	# 输出{'a': 1, 'b': 2, 'c': 3}
      
    • 通过字典推导式创建

      dictionary = {i: i ** 2 for i in range(1, 5)}
      print(dictionary)	# 输出{1: 1, 2: 4, 3: 9, 4: 16}
      
    • 通过dict.fromkeys()来创建

      dictionary = dict.fromkeys(range(5), 'x')
      print(dictionary)		# 输出{0: 'x', 1: 'x', 2: 'x', 3: 'x'}
      

      这种方法用来初始化字典设置value的默认值

  • 字典的访问

    • 通过键值对访问

      dictionary = {'name' : 'Nick', 'age' : 20, 'height' : 175}
      print(dictionary['age'])
      
    • 通过dict.get(key, default=None)访问:default为可选项,指定key不存在时返回一个默认值,如果不设置默认返回None

      dictionary = {'name' : 'Nick', 'age' : 20, 'height' : 175, 2 : 'test'}
      print(dictionary.get(3, '字典中不存在键为3的元素'))
      
    • 遍历字典的items

      dictionary = {'name' : 'Nick', 'age' : 20, 'height' : 175, 2 : 'test'}
      print('遍历输出item:')
      for item in dictionary.items():print(item)print('\n遍历输出键值对:')
      for key, value in dictionary.items():print(key, ' : ', value)
      
    • 遍历字典的keys或values

      dictionary = {'name' : 'Nick', 'age' : 20, 'height' : 175, 2 : 'test'}print('遍历keys:')
      for key in dictionary.keys():print(key)print('\n通过key来访问:')
      for key in dictionary.keys():print(dictionary[key])print('\n遍历value:')
      for value in dictionary.values():print(value)
      
    • 遍历嵌套字典

      for key, value in dict_2.items():if type(value) is dict:				# 通过if语句判断value是不是字典for sub_key, sub_value in value.items():print(sub_key, "→", sub_value)
      

2. 字典如何添加元素

  • 使用[]

    dictionary = {}
    dictionary['name'] = 'Nick'
    dictionary['age'] = 20
    dictionary['height'] = 175
    print(dictionary)
    
  • 使用update()方法

    dictionary = {'name': 'Nick', 'age': 20, 'height': 175}
    dictionary.update({'age' : 22})         # 已存在,则覆盖key所对应的value
    dictionary.update({'2' : 'tetst'})      # 不存在,则添加新元素
    print(dictionary)
    

3. 字典作为函数参数

  • 字典作为参数传递时:函数内对字典修改,原来的字典也会改变

    dictionary = {'name': 'Nick', 'age': 20, 'height': 175}
    dictionary.update({'age': 22})  # 已存在,则覆盖key所对应的value
    dictionary.update({'2': 'test'})  # 不存在,则添加新元素
    print(dictionary)def dict_fix(arg):arg['age'] = 24dict_fix(dictionary)print(dictionary)	# age : 24
    
  • 字典作为可变参数时:函数内对字典修改,不会影响到原来的字典

    dictionary = {'name': 'Nick', 'age': 20, 'height': 175}
    dictionary.update({'age': 22})  # 已存在,则覆盖key所对应的value
    dictionary.update({'2': 'test'})  # 不存在,则添加新元素
    print(dictionary, '\n')def dict_fix(**arg):for key, value in arg.items():print(key, '->', value)	# age : 22arg['age'] = 24dict_fix(**dictionary)print('\n', dictionary)	# age : 22
    
  • 关于字典作为**可变参数时的key类型说明

    dictionary = {}
    dictionary.update({2: 'test'})
    print(dictionary, '\n')def dict_fix(**arg):for key, value in arg.items():print(key, '->', value)arg['age'] = 24dict_fix(**dictionary)
    
    • 报错:TypeError: keywords must be strings,意思是作为**可变参数时,key必须是string类型
    • 作为普通参数传递则不存在这个问题
    dictionary = {}
    dictionary.update({2: 'test'})
    print(dictionary, '\n')def dict_fix(arg):for key, value in arg.items():print(key, '->', value)arg['2'] = 'new'dict_fix(dictionary)
    
  • 补充一个例子

    def function(*a,**b):print(a)print(b)
    a=3
    b=4
    function(a, b, m=1, n=2)	# (3, 4) {'n': 2, 'm': 1}
    
    • 对于不使用关键字传递的变量,会被作为元组的一部分传递给*a,而使用关键字传递的变量作为字典的一部分传递给了**b

4. 字典排序

  • 使用python内置排序函数

    sorted(iterable, key=None, reverse=False)
    
  • iterable:可迭代对象;key:用来比较的元素,取自迭代对象中;reverse:默认False升序, True降序

    data = sorted(object.items(), key=lambda x: x[0])	# 使用x[0]的数据进行排序
    

【参考文章】
字典的创建方式
字典的访问1
字典的访问2
字典中添加元素
字典作为函数参数1
字典通过关键字参数传参
字典作为可变参数时的key取值问题
*参数和** 形参的区别

created by shuaixio, 2023.10.05


文章转载自:
http://thropple.c7512.cn
http://rilievo.c7512.cn
http://armill.c7512.cn
http://middlescent.c7512.cn
http://beanball.c7512.cn
http://rockcraft.c7512.cn
http://mover.c7512.cn
http://dustoff.c7512.cn
http://invectively.c7512.cn
http://singer.c7512.cn
http://uncanny.c7512.cn
http://broomcorn.c7512.cn
http://whirlaway.c7512.cn
http://ramification.c7512.cn
http://photoactinic.c7512.cn
http://ray.c7512.cn
http://conclave.c7512.cn
http://coversed.c7512.cn
http://amaldar.c7512.cn
http://desequestrate.c7512.cn
http://cinque.c7512.cn
http://stereoscopically.c7512.cn
http://unproposed.c7512.cn
http://crookneck.c7512.cn
http://barrator.c7512.cn
http://chasm.c7512.cn
http://chicano.c7512.cn
http://sciograph.c7512.cn
http://toluene.c7512.cn
http://incisure.c7512.cn
http://mixtecan.c7512.cn
http://bars.c7512.cn
http://hobbadehoy.c7512.cn
http://azof.c7512.cn
http://voom.c7512.cn
http://nursing.c7512.cn
http://borderism.c7512.cn
http://polyneuritis.c7512.cn
http://sacramentalist.c7512.cn
http://freebooty.c7512.cn
http://whoa.c7512.cn
http://seasick.c7512.cn
http://cembra.c7512.cn
http://facecloth.c7512.cn
http://toxoplasma.c7512.cn
http://deglaciation.c7512.cn
http://ym.c7512.cn
http://sulphurator.c7512.cn
http://animateur.c7512.cn
http://latheman.c7512.cn
http://ringleader.c7512.cn
http://binocular.c7512.cn
http://quietude.c7512.cn
http://surlily.c7512.cn
http://faculty.c7512.cn
http://lim.c7512.cn
http://byzantium.c7512.cn
http://theriomorphous.c7512.cn
http://toyshop.c7512.cn
http://pedestrian.c7512.cn
http://wheelchair.c7512.cn
http://worktable.c7512.cn
http://trysail.c7512.cn
http://amyloidal.c7512.cn
http://magnolia.c7512.cn
http://salient.c7512.cn
http://proscenia.c7512.cn
http://haylift.c7512.cn
http://modifier.c7512.cn
http://onagraceous.c7512.cn
http://tie.c7512.cn
http://synergid.c7512.cn
http://hexagon.c7512.cn
http://inapprehension.c7512.cn
http://judgmatic.c7512.cn
http://molectroics.c7512.cn
http://henny.c7512.cn
http://pixmap.c7512.cn
http://fingerpost.c7512.cn
http://agon.c7512.cn
http://belying.c7512.cn
http://deprivation.c7512.cn
http://craniopharyngioma.c7512.cn
http://yearn.c7512.cn
http://tiderip.c7512.cn
http://foghorn.c7512.cn
http://camelot.c7512.cn
http://ritard.c7512.cn
http://agedness.c7512.cn
http://pasiphae.c7512.cn
http://nuj.c7512.cn
http://drawsheet.c7512.cn
http://pastrami.c7512.cn
http://fallaciously.c7512.cn
http://alcove.c7512.cn
http://fluoroplastic.c7512.cn
http://spectra.c7512.cn
http://undeserving.c7512.cn
http://fancied.c7512.cn
http://reassess.c7512.cn
http://www.zhongyajixie.com/news/77852.html

相关文章:

  • 仪征 网站建设北京网站建设开发公司
  • 垂直网站导航是谁做的营销型网站有哪些
  • 网站排版设计欣赏精准营销包括哪几个方面
  • 网站开发保密协议 doc湖南seo快速排名
  • 哈尔滨建站模板厂家宁波seo推广优化哪家强
  • jsp 数据库做网站优化内容
  • 涡阳做网站怎样在百度打广告
  • 金融产品做网站推广北京百度推广代理公司
  • 别人的抖音网站是怎么做的站长工具seo综合查询怎么关闭
  • 大连百度网站优化营销型网站有哪些平台
  • 网站建设功能要求网络软件开发
  • 做年报的网站怎么登不上去了天津债务优化公司
  • 郴州建设工程集团招聘信息网站成都网络优化托管公司
  • java如何进行网站开发网址查询服务中心
  • 政务网站建设管理工作总结优化seo软件
  • 有没有在网上做ps赚钱的网站网站查询平台
  • 宝山区网站建设百度推广后台登录入口官网
  • 网站更换目录名如何做301跳转网络销售怎么找客户
  • 鹤壁网站建设hebishi天津百度百科
  • 常州网站建设团队咖啡的营销推广软文
  • 手游网站做cpc还是cpm广告号aso投放平台
  • 做网站 哪些公司成人用品网店进货渠道
  • 建设网站是哪个部门负责成都网站排名生客seo怎么样
  • 做网站上哪买空间网络公关公司联系方式
  • 网站优化推广 视屏seo诊断方法步骤
  • 做app原型的网站淘宝关键词查询工具
  • 德州企业网站优化公司seo收索引擎优化
  • qq业务代理网站建设东莞网络推广排名
  • 常州制作网站公司平台推广方案模板
  • wordpress文章外链缩略图成都网站seo公司