• Python Base Three


    //sixth day to study python(2016/8/7)

    32. In python , there are have an special type dictionary , it is same with oc.

          such as:

          dicOne = {'wyg':'write code change world', 'roy':'you cand do it', 'tom':'just do it'}

          dicOne

          ->

          {'wyg':'write code change world', 'roy':'you cand do it', 'tom':'just do it'}

          dicOne['wyg']

          ->

          'wirte code change world'

          dicOne['wyg'] = 'believe youself'

          dicOne

          ->

          {'wyg':'believe youself', 'roy':'you cand do it', 'tom':'just do it'}

          dicTwo = dict(wyg = 'just do it', roy = 'you can do it')

          dicTwo

          ->

          {'wyg':'just do it', 'roy':'you can do it'}

          dicThree = dict((('r':'rrr'),('t':'ttt')))

          dicThree

          ->

          {'r':'rrr','t':'ttt'}

    33. In python ,dictionary type we can use everywhere, so we should learn it more deeply.

          fromkeys()

          dict1 = {}

          dict1.fromkeys((1, 2, 3))

          ->

          {1:None, 2:None, 3:None}

          dict1.fromkeys((1,2,3),'Number')

          ->

          {1:'Number', 2:'Number', 3:'Number'}

          dict1.fromkeys((1,2,3),('one','two','three'))

          ->

          {1: ('one', 'two', 'three'), 2: ('one', 'two', 'three'), 3: ('one', 'two', 'three')}

          keys()

          dict1 = dict.fromkey(range(5),'roy')

          dict1

          ->

          {0:'roy',1:'roy',2:'roy',3:'roy',4:'roy'}

          for eachKey in dict1.keys():

               print(eachKey)

          ->

          0

          1

          2

          3

          4

          5

          values()

          for eachValue dict1.values():

               print(eachValue)

          ->

          roy

          roy

          roy

          roy

          roy

          items()

          for eachItem in dict1.items():

               print(eachItem)

          ->

          (0, 'roy')
          (1, 'roy')
          (2, 'roy')
          (3, 'roy')
          (4, 'roy')

          get()
          dict1.get(0)

          ->

          'roy'

          print(dict1.get(100))

          ->

          None

          dict1.get(1,'no keyvalue')

          ->

          'roy'

          dict1.get(100,'no keyvalue')

          ->

          'no keyvalue'

          in , not in (key)

          3 in dict1

          True

          100 in dict1

          False

          clear()

          dict1.clear()

          ->

          {}
          copy() (light copy)

          a = {1:'one',2:'two'}

          b = a.copy()

          c = a

          id(a)    id(b)   id(c)

          ->

         4346314824     4386886856     4346314824

         c[3] = 'three'

         a

        ->

        {1:'one',2:'two',3:'three'}

        b

        ->

        {1:'one',2:'two'}

        c

        ->

        {1:'one',2:'two',3:'three'}

        pop()
        a.pop(2)

        ->

        {1:'one',2:'three'}

        popitem()

        a.popitem()

        -> rand pop an object

        setdefault()

        a = {1:'one'}

        a.setdefault(2)

        a

        -> 

        {1:'one',2:None}

        a.setdefault(3,'three')

        {1:'one',2:None,3:'three}

        update()
        b = {'roy':'wyg'}

        a.update(b)

        ->

        {1:'one',2:None,3:'three,'roy':'wyg'}

    34. we have learned dictionary ,now we learn set continue.

          num = {}

          type(num)

          <class 'dict' at 0x100229b60>

          num = {1,2,3}

          type(num)

         <class 'set' at 0x10022e420>

          in set ,all value is only but no support index. such as:

          num2 = {1,2,3,4,5,5,6}

          num2

          ->

          {1,2,3,4,5,6}

          num3 = set([1,2,3,4])

          num3

          ->

          {1,2,3,4}

           now how can remove repeat value from list

           such as:

           a = [1,2,3,4,5,5,6]

           b = []

           for each in a:

                if each not in b:

                          b.append(each)

           b

           ->

           [1,2,3,4,5,6]

           now that we have learned set ,how to achieve it by set

           a = list(set(a))

           ->

           [1,2,3,4,5,6]

          

     

          

       

       

        

  • 相关阅读:
    Python 基础 -2.4.2 生成器,迭代器
    Python 基础 -2.4.1 装饰器
    Python 基础 -2.4 函数进阶,名称空间,闭包,高阶函数,递归,匿名函数,生产式,生成器,迭代器
    Python 基础 -2.3 函数
    python中字典,元组,列表和字符串之间的转换
    input和raw_input区别
    Python 基础 -2.2 文件操作
    Golang之AES/DES加密解密
    Golang与MySQL
    RTFM
  • 原文地址:https://www.cnblogs.com/wangyaoguo/p/5745616.html
Copyright © 2020-2023  润新知