开始Python -- List和Tuple(2)

来源:互联网 发布:erp软件是什么软件 编辑:程序博客网 时间:2024/05/22 07:08

2List

1 list函数

l         根据String创建List

>>> list('Hello')

['H', 'e', 'l', 'l', 'o']

l         转换字符ListString

''.join(somelist)

2)基本 List操作符

l         改变List

>>> x = [1, 1, 1]

>>> x[1] = 2

>>> x

[1, 2, 1]

l         删除元素:del

>>> names = ['Alice', 'Beth', 'Cecil', 'Dee-Dee', 'Earl']

>>> del names[2]

>>> names

['Alice', 'Beth', 'Dee-Dee', 'Earl']

l         Slice赋值:

>>> name = list('Perl')

>>> name[1:] = list('ython')

>>> name

['P', 'y', 't', 'h', 'o', 'n']

l         使用Slice赋值插入元素:

 

>>> numbers = [1, 5]

>>> numbers[1:1] = [2, 3, 4]

>>> numbers

[1, 2, 3, 4, 5]

l         使用Slice赋值删除元素:

>>> numbers[1:4] = []

>>> numbers

[1, 5]

3 List函数

l         append:追加对象到List结尾

>>> lst = [1, 2, 3]

>>> lst.append(4)

>>> lst

[1, 2, 3, 4]

l         count:统计元素在List中出现的次数

>>> ['to', 'be', 'or', 'not', 'to', 'be'].count('to')

2

l         extend:追加SequenceList结尾

>>> a = [1, 2, 3]

>>> b = [4, 5, 6]

>>> a.extend(b)

>>> a

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

l         相比a + bextend会改变a的结果,而a + b却不会改变a的结果:

>>> a = [1, 2, 3]

>>> b = [4, 5, 6]

>>> a + b

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

>>> a

[1, 2, 3]

l         可以用a = a + b来实现extend的功能,但效率没有extend

l         index:返回List中第一次出现该元素的index值,找不到抛异常

>>> knights = ['We', 'are', 'the', 'knights', 'who', 'say', 'ni']

>>> knights.index('who')

4

>>> knights.index('herring')

Traceback (most recent call last):

  File "<interactive input>", line 1, in ?

ValueError: list.index(x): x not in list

l         insert:插入一个对象到List

>>> numbers = [1, 2, 3, 5, 6, 7]

>>> numbers.insert(3, 'four')

>>> numbers

[1, 2, 3, 'four', 5, 6, 7]

l         可以使用Slice赋值实现相同的功能:

>>> numbers = [1, 2, 3, 5, 6, 7]

>>> numbers[3:3] = ['four']

>>> numbers

[1, 2, 3, 'four', 5, 6, 7]

l         pop:从List中移去一个元素(缺省是最后一个),并返回它

>>> x = [1, 2, 3]

>>> x.pop()

3

>>> x

[1, 2]

>>> x.pop(0)

1

>>> x

[2]

l         可以使用pop()函数实现堆栈功能,虽然Python不提供相应的push函数,但可以使用append()函数实现

l         remove:移去List中遇到的第一个元素(和pop()函数不同,不返回该元素)

>>> x = ['to', 'be', 'or', 'not', 'to', 'be']

>>> x.remove('be')

>>> x

['to', 'or', 'not', 'to', 'be']

l         reverse:反转List中元素

>>> x = [1, 2, 3]

>>> x.reverse()

>>> x

[3, 2, 1]

l         reverse()函数会改变List,如果不想改变List,可以使用reversed()函数;由于reversed()函数返回的不是List,需要用list()函数转换:

>>> x = [1, 2, 3]

>>> list(reversed(x))

[3, 2, 1]

l         sort:对List进行排序,会改变List,但不返回任何值

>>> x = [4, 6, 2, 1, 7, 9]

>>> print x.sort()

None

>>> x

[1, 2, 4, 6, 7, 9]

l         如果不想改变原来的List,可以先拷贝List,再进行排序:

>>> x = [4, 6, 2, 1, 7, 9]

>>> y = x[:]

>>> y.sort()

>>> x

[4, 6, 2, 1, 7, 9]

>>> y

[1, 2, 4, 6, 7, 9]

l         注意,不要使用y = x的赋值,这样会使yx指向同一个List

>>> y = x

>>> y.sort()

>>> x

[1, 2, 4, 6, 7, 9]

>>> y

[1, 2, 4, 6, 7, 9]

l         另一种对拷贝List排序的方法是使用sorted()函数:

>>> x = [4, 6, 2, 1, 7, 9]

>>> y = sorted(x)

>>> x

[4, 6, 2, 1, 7, 9]

>>> y

[1, 2, 4, 6, 7, 9]

l         sorted()函数返回的是List

>>> sorted('Python')

['P', 'h', 'n', 'o', 't', 'y']

l         高级排序:可以以compare(x,y)的形式(x<y时返回负数,x=y时返回0x>y时返回正数)定义自己的比较函数,作为参数传递给sort()函数,下面的例子使用內建的cmp()函数提供缺省的行为:

>>> cmp(42, 32)

1

>>> cmp(99, 100)

-1

>>> cmp(10, 10)

0

>>> numbers = [5, 2, 9, 7]

>>> numbers.sort(cmp)

>>> numbers

[2, 5, 7, 9]

l         按元素的长度排序:

>>> x = ['aardvark', 'abalone', 'acme', 'add', 'aerate']

>>> x.sort(key=len)

>>> x

['add', 'acme', 'aerate', 'abalone', 'aardvark']

l         反转排序(缺省为升序,反转为降序):

>>> x = [4, 6, 2, 1, 7, 9]

>>> x.sort(reverse=True)

>>> x

[9, 7, 6, 4, 2, 1]

 

 
原创粉丝点击