Python data structure

1. append(x) = > a[len(a):] = [x],

    extend(L) => a[len(a):] = L,

    insert(i,x)  # before i,

    remove(x) # remove x or return error,

    pop([i]) # default value is the last element, and return that.

    count(x) # count the appearance times

    sort()

    reverse()

2. as a stack

    a.append(),a.pop() => push,pop

3. as a queue

    a.append(),a.pop(0)=>push_back,pop_front

4. filter(function,sequence)=> each item call function, if that function return true,then the answer include this item

    map(function,sequence)=> each item call function, if that is true then add to the answer linked list

    map(None,list1,list2) => make two list to be a couple

    reduce(function,sequence,[initial val]) => first two item call this function,and then call the function with returned item and the next item,and to the end, when there is only on item then return itself, if sequence is none then throw a exception except there is a initial value

5. derive list

    vec = [2,4,5]

    [x*3 for x in vec if x>3]

    [[x,x**3] for x in vec]

    [(x,x**3) for x in vec] # tuples

    [x*y for x in vec1 for y in vec2]

    [x+y for x in vec1 for y in vec2]

    [vec1[i]*vec2[i] for i in range(len(vec1))]

6. del

    del a[0]

    del a[2:4] # third and forth item

    del a

7. tuples

    tuple must be nested and can not be changed like string

    empty tuple: empty = (), singleton = 'hello', # comma is needed

    inverse operation: t = 233,'dasfd',323  x,y,z = t

8. dictionary

     associate memories, associate arrays

     key:values

     t = {'man':0,'woman':1,'child':2}

     t['man'],t.has_key('woman') return true

     construct a dictionary use list tuple

     dict([(x,x*x) for x in vec])

     traverse a dictionary

     for k,v in t.items():

             print k,v

9. use sequence

    for i,v in enumerate(['adfd','dklfj','sdf']):

               print i,v

10. two or more sequence

      for q,a in zip(questions,answers):

            print 'what is your %s, it is %s.' %(q,a)

11. compare operation can pass

       a < b == c (a<b && b==c)

       not > and  > or

       A and not B or C(A & !B or C)

       string1, string2,string3 = '','abc','def'

       non_null = string1 or string2 or string3

12. compare

       (1,2,3) <  (1,2,4)

       [1,2,3] < (1,2,4)

       'abc'<'c'<'d'

        (1,2,3,4) < (2)

       (1,2) < (1,2,-1)

       (1,2,3) == (1.0,2.0,3.0)

       can compare different types,

       list < string < tuple



    


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值