Python学习笔记——列表

1.创建列表类型数据并给其赋值

>>> aList = [123,'abc',4.56,['inner','list'],7-9j]
>>> aList
[123, 'abc', 4.56, ['inner', 'list'], (7-9j)]
>>> list('ABC')
['A', 'B', 'C']

2.访问列表中的值

>>> aList[0]
123
>>> aList[1:4]
['abc', 4.56, ['inner', 'list']]
>>> aList[:3]
[123, 'abc', 4.56]
>>> aList[3][1]
'list'

 3.如何更新列表

>>> aList[2]='ABC'
>>> aList
[123, 'abc', 'ABC', ['inner', 'list'], (7-9j)]
>>> aList.append("Hello Word")
>>> aList
[123, 'abc', 'ABC', ['inner', 'list'], (7-9j), 'Hello Word']

4.删除列表中的元素或者列表本身

>>> del aList[1]
>>> aList
[123, 'ABC', ['inner', 'list'], (7-9j), 'Hello Word']
>>> aList.remove(123)
>>> aList
['ABC', ['inner', 'list'], (7-9j), 'Hello Word']
>>> del aList

5.标准类型操作符

列表比较操作的时候,是两个列表的元素分别比较,直到有一方的元素胜出

6.序列类型操作符

  <1>切片([]和[:])

>>> aList
[123, 'abc', 'ABC', ['inner', 'list'], (7-9j), 'Hello Word']
>>> aList[-2:-1]
[(7-9j)]
>>> aList[2:-1]
['ABC', ['inner', 'list'], (7-9j)]

    <2>成员关系操作(in,not in)

    输出的是True或者False

  <3>连接操作符(+)

>>> aList=[1,2,3,4]
>>> bList=['A','B','C','D']
>>> cList = aList+bList
>>> cList
[1, 2, 3, 4, 'A', 'B', 'C', 'D']

   <4>重复操作符(*)

>>> aList * 2
[1, 2, 3, 4, 1, 2, 3, 4]

7.标准类型函数cmp()

  比较的规则P140

8.序列类型函数

  <1>len()——对列表和元组返回的是列表或者元组的元素个数

    <2>max()和min()——比较大小

  <3>sorted()和reversed()—— 

>>> bList
['A', 'B', 'C', 'D']
>>> for t in reversed(bList):
...     print t
... 
D
C
B
A
>>> bList=['D','C','B','A']
>>> sorted(bList)
['A', 'B', 'C', 'D']

   <4>enumerate()和zip()——

['A', 'B', 'C', 'D']
>>> for i,t in enumerate(bList):
...     print i,t
... 
0 A
1 B
2 C
3 D
>>> aList
[1, 2, 3, 4]
>>> bList
['A', 'B', 'C', 'D']
>>> for i,j in zip(aList,bList):
...     print ('%s %s' % (i,j)).title()
... 
1 A
2 B
3 C
4 D

   <5>sum()

>>> aList
[1, 2, 3, 4]
>>> sum(aList)
10

   <6>list()和tuple()——用于列表和元组之间的转换

9.列表类型的内建函数——P142

>>> dir(aList)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

>>> aList
[1, 2, 3, 4]
>>> aList.append('A')
>>> aList
[1, 2, 3, 4, 'A']
>>> aList.count('A')
1
>>> aList.insert(1,1.5)
>>> aList
[1, 1.5, 2, 3, 4, 'A']
>>> 'A' in aList
True
>>> aList.index(1.5)
1

>>> aList.sort()
>>> aList
[1, 1.5, 2, 3, 4, 'A']
>>> aList.reverse()  #没有返回值的函数会直接改变对象的值
>>> aList
['A', 4, 3, 2, 1.5, 1]

>>> bList.extend(aList)
>>> bList
['A', 'B', 'C', 'D', 'A', 4, 3, 2, 1.5, 1]

 

10.append和extend

 

转载于:https://www.cnblogs.com/tonglin0325/p/5736920.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值