Python 序列

       Python中sequence主要包含存储单个元素序列和两个元素对的序列,str就是一个字符容器。

      单元素序列主要有以下类型:

              bytearray: 字节数组,通过built-in 函数bytearray()创建
      xrange:由函数xrange(n)/xrange(start, stop, step)创建,但不支持容器上的切片,连接,复制, in/not in 判断,                min/max取值操作,xrange是 immutable sequence, xrange对象大小固定,与其代表范围大小无关,xrange主要用于循
             环,比range效率稍高,xrange上的操作只支持 indexing,  iteration, len
       list:链表mutable, [a,b,c],相当于其他语言中的数组链表集合体,因此也就不分LinkList和ArrayList
       tuple: immutable list,非空tuple括号可有可无a,b,c 或(a,b,c) , empty tuple必须有括号(),  single item tuple 逗               号必不可少 a, 或(a,);不能对tuple的某个元素赋值,但tuple中的元素可以是mutable
       set:  mutable unordered hashtable, 支持数学操作并集合集等,因为无序,所以不支持indexing,slicing等顺序性操作              非空{a,b,c} 空set(),支持issubset/union/intersection/difference
       frozenset: immutable unordered hashtable

      双元素序列则主要是关联数组:

 dict: hash_table, comma-separated list of key-value: {a:b, c:d} , empty dic: dic(),主要有以下方法:

            iter(d) is short for iterkeys()

             get(key)/has_key(key)/pop(key)/popitem()
             items(): return a copy of the dictionary's list of (key, value) pairs
             keys(): return a list of keys
             sorted(keys()):  return a sorted list of keys
      dictview: dict.viewkeys()/viewvalues()/viewitems() 返回,是dict的view,即dict变了,view也会变

   Sequence主要操作就是增删改查,以下查操作优先级依次递增,s、t类型相同,n、i、j 是整形
Operation Result Notes
x in s True if an item of s is equal to x, else False (1)
x not in s False if an item of s is equal to x, else True (1)
s + t the concatenation of s and t (6)
s * n, n * s n shallow copies of s concatenated (2)
s[i] ith item of s, origin 0 (3)
s[i:j] slice of s from i to j (3)(4)
s[i:j:k] slice of s from i to j with step k (3)(5)
len(s) length of s  
min(s) smallest item of s  
max(s) largest item of s  
s.index(x) index of the first occurrence of x in s  
s.count(x) total number of occurrences of x in s  
        容器之间也可以进行比较,依次比较容器中的每一个元素
        如果s是str/unicode,  in/not in 判断x是否是s的字串
        n<0时s*n = s*0 返回类型与s相同的空容器
        如果i/j < 0, s[i/j] 的位置为len(s) + i/j,  但s[0]=s[-0]
        i,j都可以省略,省略i则i=0, 省略j 则j=len(s), 如果i/j>len(s), 则取len(s), 如果i>=j, 则slice返回空

    Sequence按可变性又可分为mutable和immutable:
        immutable sequence: string, xrange, tuple, 一旦创建,不能再改变
        mutable sequence: bytearray, list 可以添加元素或改变某个位置的元素,可变容器支持以下操作
Operation Result Notes
s[i] = x item i of s is replaced by x  
s[i:j] = t slice of s from i to j is replaced by the contents of the iterable t  
del s[i:j] same as s[i:j] = []  
s[i:j:k] = t the elements of s[i:j:k] are replaced by those of t (1)
del s[i:j:k] removes the elements of s[i:j:k] from the list  
s.append(x) same as s[len(s):len(s)] = [x] (2)
s.extend(x) same as s[len(s):len(s)] = x (3)
s.count(x) return number of i‘s for which s[i] == x  
s.index(x[, i[, j]]) return smallest k such that s[k] == x and i <= k < j (4)
s.insert(i, x) same as s[i:i] = [x] (5)
s.pop([i]) same as x = s[i]; del s[i]; return x (6)
s.remove(x) same as del s[s.index(x)] (4)
s.reverse() reverses the items of s in place (7)
s.sort([cmp[, key[, reverse]]]) sort the items of s in place (7)(8)(9)(10)
       t的长度需与slice长度相等
       x可以为任何iterable 类型

    Sequence遍历:
      for x in s:...
      enumerate(): retrieve the position index and corresponding value  at the same time
      zip():  loop over two or more sequences at the same time
      iteritems(): retrieve  the key and corresponding value at the same time while  looping through dictionaries
      遍历集合时若需对集合进行修改,最好遍历集合的副本,slice[:]很容易获取集合的copy


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值