列表和元组

    xiaoxiao2021-03-25  7

    最基本的数据结构是序列(sequence),序列中的每个元素被分配一个序号—-即元素的位置,也称索引。python包含6中内建的序列,最常用的有两种:列表和元组,列表和元组的区别在于,列表可以修改,元组则不能。 通用序列操作 所有的序列类型都可以进行某些特定的操作。这些操作包括:索引(indexing),分片(sliceing),加(adding),乘(multipying)以及检查某个元素是否属于序列的成员。 索引

    >>>a="hello" >>>a[0] 'h'

    分片

    >>> a[0:3] 'hel' >>> a[0:] 'hello' >>> a[0:6] 'hello' >>> a[:1] 'h' >>> a[:-1] 'hell'

    步长

    >>> numbers=[1,2,3,4,5,6,7,8,9,10] >>> numbers[0:10:2] [1, 3, 5, 7, 9] >>> numbers[0:10:3] [1, 4, 7, 10] >>> numbers[0:10:-1] [] >>> numbers[10:0:-1] [10, 9, 8, 7, 6, 5, 4, 3, 2] >>> numbers[10:0:-2] [10, 8, 6, 4, 2] >>> numbers[10:0] []

    序列相加

    >>> [1,2,3] + [4,3,2] [1, 2, 3, 4, 3, 2] >>> 'hello' + 'world' 'helloworld'

    乘法

    >>> ['hello','world']*2 ['hello', 'world', 'hello', 'world'] >>> [1,2,3]*2 [1, 2, 3, 1, 2, 3] >>> [None]*10 [None, None, None, None, None, None, None, None, None, None]

    成员资格 in

    >>> users=['jim','tom','jack','fire'] >>> raw_input('Enter your user name: ') in users Enter your user name: fire True >>> username=raw_input('User name: ') User name: jim >>> pin = raw_input('PIN code: ') PIN code: 923 >>> if [username,pin] in database: print 'Access granted' ... Access granted

    长度,最大值,最小值

    >>> a=[34,56,88,100] >>> max(a) 100 >>> min(a) 34 >>> len(a) 4

    赋值

    >>> x=[1,1,2] >>> x[0]=100 >>> x [100, 1, 2]

    list函数

    >>> list('helloworld') ['h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd']

    删除元素

    >>> a=['xshow','jim','fire','jack'] >>> del a[0] >>> a ['jim', 'fire', 'jack']

    分片赋值

    >>> name=list('xshow') >>> name ['x', 's', 'h', 'o', 'w'] >>> name[1:]='boy' >>> name ['x', 'b', 'o', 'y'] >>> name=['xshow','fire'] >>> name ['xshow', 'fire'] >>> name[1:1]='girl' >>> name ['xshow', 'g', 'i', 'r', 'l', 'fire'] >>> name[1:1]=['girl'] >>> name ['xshow', 'girl', 'g', 'i', 'r', 'l', 'fire'] >>> numbers=[1,2,3,4,5] >>> numbers[1:3]=[] >>> numbers [1, 4, 5]

    列表方法 append

    >>> a=[] >>> a.append('xshow') >>> a ['xshow'] >>> a.append('fire') >>> a ['xshow', 'fire']

    count

    >>> b=[[1,2],1,1,[2,1,[1,2]]] >>> b.count(1) 2 >>> b.count([1,2]) 1

    extend

    >>> a=['to','be','not','be'] >>> b=['xshow','fire','jim'] >>> a.extend(b) >>> a ['to', 'be', 'not', 'be', 'xshow', 'fire', 'jim']

    index

    >>> a=['to','be','not','to','be'] >>> a.index('not') 2

    insert

    >>> a=['to','be','not','to','be'] >>> a.insert(1,'xshow') >>> a ['to', 'xshow', 'be', 'not', 'to', 'be']

    pop

    >>> a=['to','be','not','to','be'] >>> a.pop() 'be' >>> a ['to', 'be', 'not', 'to'] >>> a=[1,2,3,4] >>> a.append(a.pop()) >>> a [1, 2, 3, 4]

    remove

    >>> a=['to','be','not','to','be'] >>> a.remove('to') >>> a ['be', 'not', 'to', 'be']

    reverse

    >>> a=['to','be','not','to','be'] >>> a.reverse() >>> a ['be', 'to', 'not', 'be', 'to'] >>> b=[1,2,5,4,6,8] >>> b.reverse() >>> b [8, 6, 4, 5, 2, 1]

    sort 修改原列表返回空值

    >>> b=[1,2,5,4,6,8] >>> b.sort() >>> b [1, 2, 4, 5, 6, 8] >>> sorted('helloworld') ['d', 'e', 'h', 'l', 'l', 'l', 'o', 'o', 'r', 'w'] >>> x=[1,3,2,344,546,0] >>> y=x[:] >>> y.sort() >>> y [0, 1, 2, 3, 344, 546] >>> x [1, 3, 2, 344, 546, 0]

    不改变原列表,生成新列表

    >>> y=sorted(x) >>> y [0, 1, 2, 3, 344, 546] >>> x [0, 1, 2, 3, 344, 546]

    cmp

    >>> cmp(42,12) 1 >>> cmp(42,42) 0 >>> cmp(32,42) -1 >>> numbers=[5,2,9,7] >>> numbers.sort(cmp) >>> numbers [2, 5, 7, 9]

    高级排序

    >>> a=[2,4,5,1,3] >>> a [2, 4, 5, 1, 3] >>> sorted(a).reverse() >>> a [2, 4, 5, 1, 3] >>> a=['xshow','hellowprd','girl','boy'] >>> a.sort(key=len) >>> a ['boy', 'girl', 'xshow', 'hellowprd'] >>> b=[1,4,2,3,5,87] >>> b.sort() >>> b [1, 2, 3, 4, 5, 87] >>> b.sort(reverse=True) >>> b [87, 5, 4, 3, 2, 1]

    元组 元组的创建

    >>> 1,2,3 (1, 2, 3) >>> (1,2,3) (1, 2, 3) >>> () () >>> 1, (1,) >>> 3*(42+2) 132 >>> 3*(42+2,) (44, 44, 44)

    tuple

    >>> a=['xshow','fire','jim'] # 返回元组值,不改变原值 >>> tuple(a) ('xshow', 'fire', 'jim') >>> a ['xshow', 'fire', 'jim']
    转载请注明原文地址: https://ju.6miu.com/read-155305.html

    最新回复(0)