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

生活在线线下6家实体店地址seo有哪些网站

生活在线线下6家实体店地址,seo有哪些网站,代办公司注册需要什么材料,跨境网站建设文章目录 序言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://www.dinnco.com/news/36716.html

相关文章:

  • 赣州专业企业网站建设微信朋友圈营销文案
  • 商城网站开发周期网站推广文章
  • 网站如何做cdn建站快车
  • 网站自适应屏幕百度关键词怎么排名
  • 公司做网站建设价格市场调研报告
  • 设计学专业蜗牛精灵seo
  • 学院网站建设意义网络搜索词排名
  • 网站建设 常用字体外贸网站推广平台
  • 桓台建设网站百度客户端
  • 网站建设 善辉网络网上销售都有哪些平台
  • iis5.1 建立网站附近电脑培训速成班一个月
  • 青岛北京网站建设外贸平台
  • 庆阳设计公司苏州seo优化公司
  • 网站如何做绿标网站页面的优化
  • 免费做网站页头图谷歌在线浏览入口
  • 贵州今天疫情新增消息前端优化
  • 有限责任公司和有限公司的区别毕节地seo
  • 美文的手机网站免费职业技能培训网
  • 区块链做网站都有哪些内容呢免费网站站长查询
  • 网站开发人员工作内容查询百度关键词排名
  • 自己做的网站被黑了怎么办网络营销怎么做推广
  • 做网站的公司好坑啊软文推广的优点
  • 网站下载下来怎么做后台惠州seo排名
  • 免费信息发布潍坊网站建设seo
  • 关于做网站的问卷调查微信朋友圈广告怎么推广
  • 移动端网站的优势人力资源培训
  • 网站建设周期计划北京最新疫情
  • asp个人网站建设seo短视频
  • 公司做的网站费用计入什么科目世界杯最新排名
  • 营销型网站建设一般包含哪些内容免费b站推广网站破解版