用下标取得列表中的单个值
>>> spam = ['cat','bat','rat','elephant']
>>> spam[0]
'cat'
>>> spam[1]
'bat'
>>> spam[2]
'rat'
>>> spam[3]
'elephant'
>>> ['cat','bat','rat','elephant'][3]
'elephant'
>>> 'hello' + spam[0]
'hellocat'
>>> 'the ' + spam[1] + 'ate the ' + spam[0] + '.'
'the batate the cat.'
负数下标
>>> 'the ' + spam[1] + 'ate the ' + spam[0] + '.'
'the batate the cat.'
>>> spam = ['cat','bat','rat','elephant']
>>> spam[-1]
'elephant'
>>> spam[-3]
'bat'
>>> 'the ' + spam[-1] + 'is afraid of the ' + spam[-3] + '.'
'the elephantis afraid of the bat.'
利用切片取得子列表
>>> spam = ['cat', 'bat','rat','elephant']
>>> spam[0:4]
['cat', 'bat', 'rat', 'elephant']
>>> spam[1:3]
['bat', 'rat']
>>> spam[0:-1]
['cat', 'bat', 'rat']
作为快捷方法,可以省略切片中冒号两边的一个下标或两个下标。
>>> spam = ['cat','bat','rat','elephant']
>>> spam[:2]
['cat', 'bat']
>>> spam[1:]
['bat', 'rat', 'elephant']
>>> spam[:]
['cat', 'bat', 'rat', 'elephant']
用len()取得列表长度
>>> spam = ['cat','bat','rat']
>>> len(spam)
3
用下标改变列表中的值
>>> spam = ['cat','bat','rat','elephant']
>>> spam[1]='aardvark'
>>> spam
['cat', 'aardvark', 'rat', 'elephant']
>>> spam[2] = spam[1]
>>> spam
['cat', 'aardvark', 'aardvark', 'elephant']
>>> spam[-1] = 123
>>> spam
['cat', 'aardvark', 'aardvark', 123]
列表连接和列表复制
>>> [1,2,3] + ['a','b','c']
[1, 2, 3, 'a', 'b', 'c']
>>> ['a','b','c']*3
['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c']
>>> spam = [1,2,3]
>>> spam = spam + ['a','b','c']
>>> spam
[1, 2, 3, 'a', 'b', 'c']
用del语句从列表中删除值
>>> spam = ['cat','bat','rat','elephant']
>>> del spam[2]
>>> spam
['cat', 'bat', 'elephant']
>>> del spam[2]
>>> spam
['cat', 'bat']
使用列表
myNumber = []
while True:
print('输入第'+str(len(myNumber)+1) +'变量' )
name = input()
'''等于空退出循环'''
if name == '':
break
'''连接复制列表到变量'''
myNumber = myNumber + [name]
print('总共输入的变量是:'+ str(len(myNumber)) + '个')
for name in myNumber:
print(name)
列表用于循环
for i in range(4):
print(i)
for a in [0,1,2,3]:
print(a)
spam = ['cat','bat','rat','elephant']
for b in range(len(spam)):
print('下标是'+ str(b) + '对应值是' + spam[b])
in 和 not in操作符
>>> 'howdy' in ['hello','hi','howdy','heyas']
True
>>> spam = ['hello','hi','howdy','heyas']
>>> 'cat' in spam
False
>>> 'howdy' not in spam
False
>>> 'cat' not in spam
True
让程序判断名字在不在列表中
myPets = ['Zophie','Pooka','Fat-tail']
print('输入名字')
name = input()
if name not in myPets:
print('输入的名字' + name + '不在列表中')
else:
print(name + '在列表中')
多重复制技巧
>>> cat = ['fat','black','loud']
>>> size = cat[0]
>>> color = cat[1]
>>> disposition = cat[2]
>>> print(size,color,disposition)
fat black loud
>>> # 多重赋值技巧是一种快捷方式
>>> cat = ['fat','black','loud']
>>> size,color,disposition = cat
>>> print(size,color,disposition)
fat black loud
>>> # 变量的数目和列表的长度必须严格相等,否则python将给出ValueError
增强的赋值操作
>>> spam = spam + 1
>>> spam
43
>>> spam = 42
>>> spam += spam
>>> spam
84
>>> spam = 42
>>> spam += 1
>>> spam
43
>>> spam = 'hello'
>>> spam += 'world!'
>>> spam
'helloworld!'
>>> bacon = ['cat']
>>> bacon *= 3
>>> bacon
['cat', 'cat', 'cat']
用index()方法在列表中查找值
>>> spam = ['hello','hi','howdy','heyas']
>>> spam.index('hello')
0
>>> spam.index('heyas')
3
>>> spam = ['cat','cat','cat','cat']
>>> spam.index('cat')
0
>>> # 出现列表中重复的值,就返回它第一个出现的下标
用append()和insert()方法在列表中添加值
>>> spam = ['cat','dog','bat']
>>> spam.append('moose')
>>> spam
['cat', 'dog', 'bat', 'moose']
>>> # insert()方法可以在列表任意下标处插入一个值
>>> spam = ['cat','dog','bat']
>>> spam.insert(1,'hello')
>>> spam
['cat', 'hello', 'dog', 'bat']
>>> spam.insert(-1,'rat')
>>> spam
['cat', 'hello', 'dog', 'rat', 'bat']
用remove()方法从列表中删除值
>>> spam = ['cat','bat','rat','elephant']
>>> spam.remove('bat')
>>> spam
['cat', 'rat', 'elephant']
>>> # 列表中出现多次,只有第一次出现的值会被删除。
>>> spam = ['cat','cat','cat']
>>> spam.remove('cat')
>>> spam
['cat', 'cat']
用sort()方法将列表中的值排序
spam = [2,4,2.3,8,-8,-2.3]
>>> spam.sort()
>>> spam
[-8, -2.3, 2, 2.3, 4, 8]
>>> spam = ['a','e','d','b','c']
>>> spam.sort()
>>> spam
['a', 'b', 'c', 'd', 'e']
>>> # reverse 关键字参数为True,让sor按逆序排序。
>>> spam.sort(reverse=True)
>>> spam
['e', 'd', 'c', 'b', 'a']
>>> # 不能对既有数字又有字符串值的列表排序
>>> spam = [1,5,7,'adf','adsfd']
>>> spam.sort()
Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: unorderable types: str() < int()
>>> # sort()方法对字符串排序时,使用'ASCII'字符顺序
>>> spam = ['Alice','abcd','BOb','badgers','Cloor','name']
>>> spam.sort()
>>> spam
['Alice', 'BOb', 'Cloor', 'abcd', 'badgers', 'name']
>>> # 如果需要按照普通的字典顺序来排序,就在sort()方法调用时,将关键字参数key设置为str.lower
>>> spam = ['a','z','A','Z']
>>> spam.sort(key=str.lower)
>>> spam
['a', 'A', 'z', 'Z']
例子程序: 神奇8球和列表
import random
messages = [
'It is certain',
'It is decidedly so',
'Yes definitely',
'Reply hazy try again',
'Ask again later',
'Concentrate and ask again',
'My reply is no',
'Outlook not so good',
'Very doubtful'
]
print(messages[random.randint(0,len(messages) -1 )])
类似列表的类型:字符串和元组
>>> name = 'Zophie'
>>> name[0]
'Z'
>>> name[-2]
'i'
>>> name[:4]
'Zoph'
>>> 'Zo' in name
True
>>> 'z' in name
False
>>> 'p' not in name
False
>>> for i in name:
... print(i)
...
Z
o
p
h
i
e
可变和不可变数据类型
列表是‘可变的’
字符串 ‘是不可变的’
>>> name = 'Zophie a cat'
>>> name[7] = 'aaa'
Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: 'str' object does not support item assignment
>>> name = 'Zophoe a cat'
>>> newName = name[0:7] + 'aaa' + name[8:12]
>>> name
'Zophoe a cat'
>>> newName
'Zophoe aaa cat'
>>> eggs = [1,2,3]
>>> eggs = [4,5,6]
>>> eggs
[4, 5, 6]
>>> eggs = [1,2,3]
>>> del eggs[2]
>>> del eggs[1]
>>> del eggs[0]
>>> eggs.append(7)
>>> eggs.append(8)
>>> eggs.append(9)
>>> eggs
[7, 8, 9]
元组数据类型
>>> eggs = ('hello',42, 0.5)
>>> eggs[0]
'hello'
>>> eggs[1:3]
(42, 0.5)
>>> len(eggs)
3
>>> eggs = ('hello',88, 2.22)
>>> eggs[1]=99
Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
>>> type(('hello',))
<class 'tuple'>
>>> type(('hello'))
<class 'str'>
用list()和tuple()函数来转换类型
>>> tuple(['cat','name',2,3.14])
('cat', 'name', 2, 3.14)
>>> list(('cat','name',2,3.14))
['cat', 'name', 2, 3.14]
引用
>>> spam = 42
>>> cheese = spam
>>> spam = 100
>>> spam
100
>>> cheese
42
>>> spam = [0,1,2,3,4,5]
>>> cheese = spam
>>> cheese[1] = 'hello'
>>> spam
[0, 'hello', 2, 3, 4, 5]
>>> cheese
[0, 'hello', 2, 3, 4, 5]
传递引用
def eggs(name):
name.append('hello')
spam = [1,2,3]
eggs(spam)
print(spam)
copy模块的copy()和deepcopy()函数
>>> import copy
>>> spam = ['a','b','c','d']
>>> cheese = copy.copy(spam)
>>> spam
['a', 'b', 'c', 'd']
>>> cheese
['a', 'b', 'c', 'd']
>>> cheese[1]= 88
>>> cheese
['a', 88, 'c', 'd']
>>> name = [[1,2,3],['a','b','c']]
>>> cheese = copy.deepcopy(name)
>>> cheese[1][0] = 'aaa'
>>> name
[[1, 2, 3], ['a', 'b', 'c']]
>>> cheese
[[1, 2, 3], ['aaa', 'b', 'c']]