Compare dictionary of commonly used and is operating and the ==

Compare dictionary of commonly used and is operating and the ==

Dictionary of common operations

  • Dictionary of key value and composition

    dict = {}
    dict = {'name': "李明"}
  • must be a hash key (not changing)

    dict = {}
    dict['name'] = 'xiaoxiao'
    dict[(1, 'JackMa')] = 'Alibaba'
    dict["['houhou']"] = "houhou"
    print(dict)
    {"['houhou']": 'houhou', (1, 'JackMa'): 'Alibaba', 'name': 'xiaoxiao'}
    # 第一个元素的key是一个列表组成的字符串,也是不可变的元素
  • Dictionary CRUD

    '''增加元素'''
    dict  = {}
    # 方式一
    dict['name'] = "liming"
    # 方式二
    dict.setdefault('age', 18)
    # setdefault(key, value)添加元素
    dict.setdefault('age', 19)
    # 如果字典中key已经存在,setdefault再设定value将不再继续保存
    print(dict)
    dict.setdefault('age')
    print(dict)
    {'age': None}
    # 当setdefault方法只给key的时候,value默认为None
    --------------------
    '''删除元素'''
    # 方式一:pop
    dict = {'gender': 'male', 'age': 19, 'name': '李明'}
    dict.pop()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: pop expected at least 1 arguments, got 0
    dict.pop('name')
    '李明'
    print(dict)
    {'gender': 'male', 'age': 19}
    # pop方式删除和列表中的pop有一个很大的区别,字典中必须把指定的key传入pop中才能找到并删除,否则会报上边的错误。
    
    # 方式二:del
    del dict['age']
    print(dict)
    {'gender': 'male'}
    
    # 方式三:popitem
    dict = {'gender': 'male', 'age': 18, 'name': 'lizhen'}
    print(dict.popitem())
    返回值:('gender', 'male')
    # popitem()不传递任何参数,随机删除
    
    # 方式四:clear
    dict.clear()
    print(dict)
    {}
    # 清空字典
    
    --------------------
    '''修改字典'''
    # 方式一:update
    dict = {'age': 100, 'name': 'xiaoxiao'}
    dict1 = {'gender': 'male', 'age': 150}
    print(dict1)
    {'gender': 'male', 'age': 150}
    dict.update(dict1)
    print(dict)
    {'gender': 'male', 'age': 150, 'name': 'xiaoxiao'}
    dict.update({'level': 'five'})
    print(dict)
    {'gender': 'male', 'age': 150, 'name': 'xiaoxiao', 'level': 'five'}
    # 这种方式是以另外一个字典中的元素来替换掉已有的元素,括号内必须是字典,新字典中不包括的元素,愿字典不做更改,依旧保留,原字典中没有的元素将被添加
    # 方式二
    dict['age'] = 101
    print(dict)
    {'gender': 'male', 'age': 101, 'name': 'xiaoxiao', 'level': 'five'}
    # 这种方式既可以添加也可以修改某个key的值,setdefault只能添加不存在的key,并不能修改已存在的key的值。
    
    --------------------
    '''查询方式'''
    # 方式一:方括号
    dict['age']
    101
    # 方式二:get
    dict.get('age')
    dict.get('course', '')
    101
    ''
    # 注意get方式查询的话可以设定两个参数,第一个是key,第二个自定义,如果有值返回值,如果没有返回为第二个参数
    
    # 方式三:setdefault
    dict.setdefault('age')
    dict.setdefault('age', 1000)
    101
    101
    # 注意: setdefault如果只输入已有的key或者输入key和任意的value,那么将返回对应的value值
    # setdefault首先查看是否存在key,如果不存在,添加元素,如果存在返回对应key的value值
    
  • Other dictionary operations

    # 查询所有的key值,返回的是列表, 可以进行迭代循环
    dict.keys()
    ['gender', 'age', 'name', 'level']
    for i in keys:
      print(dict[i])
    male
    101
    xiaoxiao
    five
    --------------------
    # 查询所有的values, 返回的也是列表,可以进行迭代循环
    values = dict.values()
    values
    ['male', 101, 'xiaoxiao', 'five']
    --------------------
    # 查询key、value值,组成列表, 也可以迭代循环,相当于上边两个的功能的合并
    list1 = dict.items()
    list1
    [('gender', 'male'), ('age', 101), ('name', 'xiaoxiao'), ('level', 'five')]
    for key, value in list1:
      print(key, value) 
    ('gender', 'male')
    ('age', 101)
    ('name', 'xiaoxiao')
    ('level', 'five')
    
    for i in list1:
      key, value = i
      print(key, value)
    ('gender', 'male')
    ('age', 101)
    ('name', 'xiaoxiao')
    ('level', 'five')
    # items()返回的是由key和value对应的元组组成的列表。注意遍历循环两个方式,方式一直接是遍历key和value,第二种方式是把元组作为一个变量,在把元组中的内容赋值给两个对应的变量,进行输出。key, value = (1, 2)这种方式在python中被称为解构或者解包,即把一个完整的对象中的值分解并赋值给多个变量, 解构需要注意的是变量个数必须和元组中的value元组个数一致。列表也可以解构,用法一样,但是字典的解构需要注意的是解构出来的值是字典中的每一个key,并不包含value 
    
  • Dictionary nesting

    dict = {
      'name': 'xiaoxiao', 
      'gender': 'female', 
      'age': 19,
      'record': {
          '语文': 100,
          '数学': 80
      },
      'hobby': [
          {
              'name': 'fitness',
              'year': 3
          },
          {
              'name': 'football',
              'year': 1       
          }
      ]
    }
    print(dict)
    # 结果是:
    {'name': 'xiaoxiao', 'gender': 'female', 'age': 19, 'record': {'语文': 100, '数学': 80}, 'hobby': [{'name': 'fitness', 'year': 3}, {'name': 'football', 'year': 1}]}
    # 字典中可以嵌套字典、列表,可以进行多层嵌套
    print(dict['hobby'][0].get('name'))
    # 结果是:fitness

Compare and == is the

  • Numbers and strings

    '''数字的比较'''
    n1 = 1
    n2 = 1
    id(n1)
    140419597884856
    id(n2)
    140419597884856
    print(n1==n2)
    print(n1 is n2)
    # 结果是: True, True
    
    a = -6
    b = -6
    id(a)
    140419599119512
    id(b)
    140419599999240
    print(a==b)
    True
    print(a is b)
    False
    # 值相等,但是内存地址不同
    ------------------
    '''字符串的比较'''
    a= 'alexx'
    id(a)
    4316628048
    b = 'alexx'
    id(a)
    4316628048
    print(a=b)
    print(a is b)
    # 结果是: True, True
    
    str1 = '!12'
    str2 = '!12'
    id(str1)
    4316659304
    id(str2)
    4316656944
    print(str1==str2)
    True
    print(str1 is str2)
    False
    # 值相等,但是内存地址不同
    
    str1 = 'sdd'* 21
    str1
    'sddsddsddsddsddsddsddsddsddsddsddsddsddsddsddsddsddsddsddsddsdd'
    str2 = 'sdd' * 21
    str2
    'sddsddsddsddsddsddsddsddsddsddsddsddsddsddsddsddsddsddsddsddsdd'
    id(str1) == id(str2)
    False
    # 值相等,但是内存不同
  • Compare list

    list = [1,2,3]
    list1 = [1,2,3]
    id(list)
    4316616680
    id(list1)
    4316614736
    print(list ==list1)
    True
    print(list is list1)
    False
    # 值相等,但是不共用一个内存空间
  • Comparison of tuples

    tu1 = (1,2,3)
    tu2 = (1,2,3)
    id(tu1)
    4316412096
    id(tu2)
    4316277888
    print(tu1==tu2)
    True
    print(tu1 is tu2)
    False
    # 值相等,但是不共用一个内存空间
  • Compare dictionary

    dict = {'name': "xiaoxiao"}
    dict1 = {'name': 'xiaoxiao'}
    id(dict)
    4316618832
    id(dict1)
    4316609064
    print(dict==dict1)
    True
    print(dict is dict1)
    False
    # 值相等,但是不共用一个内存空间

Note

Small data pool :

Small data storage pool is specifically set to the python strings and numbers

  • Small digital data pool rules: When the number is the time between -5 to 256, a common variable as long as the memory address value is equal to
  • Rules small pool of data strings: If you have a special character, even if the values are equal, the corresponding memory address is not the same , the space is a special character
  • String single string value * equal within the same memory 20, more than 20 different memory
  • If the strings and numbers comply with the above rules, the values ​​and memory addresses are the same

== is the comparison value of the variable is equal, it is compared to that variable corresponding to the same memory address

Guess you like

Origin www.cnblogs.com/ddzc/p/12170817.html