• 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]

          

     

          

       

       

        

  • 相关阅读:
    剑指Offer(Java版)第五十题:牛客最近来了一个新员工Fish,每天早晨总是会拿着一本英文杂志, 写些句子在本子上。同事Cat对Fish写的内容颇感兴趣,有一天他向Fish借来翻看, 但却读不懂它的意思。例如,“student. a am I”
    剑指Offer(Java版)第四十九题:汇编语言中有一种移位指令叫做循环左移(ROL), 现在有个简单的任务,就是用字符串模拟这个指令的运算结果。 对于一个给定的字符序列S,请你把其循环左移K位后的序列输出。 例如,字符序列S=”abcXYZdef”,要求输出循环左移3位后的结果, 即“XYZdefabc”。是不是很简单?OK,搞定它!
    【转载】Java 内存分配全面浅析
    【记】Linux下安装JDK1.7
    【ZooKeeper】典型应用场景概览
    正则表达式工具RegexBuddy
    【基础】RandomAccess
    【JNDI】Java Naming and Directory Interface
    【AOP】Cglib动态代理实现方式
    【事务】分布式事物原理
  • 原文地址:https://www.cnblogs.com/wangyaoguo/p/5745616.html
Copyright © 2020-2023  润新知