@zhangyu756897669
2017-08-23T15:28:55.000000Z
字数 3763
阅读 597
python官方文档
[1, 2, 3]
['cat', 'bat', 'rat', 'elephant']
['hello', 3.1415, True, None, 42]
spam = ['cat', 'bat', 'rat', 'elephant']
spam
例如:
spam = ['cat', 'bat', 'rat', 'elephant']
spam[0]
```
```
spam[1]
spam[2]
spam[3]
['cat', 'bat', 'rat', 'elephant'][3]
'Hello ' + spam[0]
'The ' + spam[1] + ' ate the ' + spam[0] + '.'
spam = ['cat', 'bat', 'rat', 'elephant']
spam[1000]
Traceback (most recent call last):
File "", line 1, in
spam[10000]
IndexError: list index out of range
spam = ['cat', 'bat', 'rat', 'elephant']
spam[1]
spam[1.0]
spam[int(1.0)]
spam = [['cat', 'bat'], [10, 20, 30, 40, 50]]
spam[0]
spam[0][1]
spam[1][4]
第一个索引指示要使用的列表值,第二个索引指示列表值中的值。例如,spam 0打印“bat”,第一个列表中的第二个值。如果只使用一个索引,程序将打印该索引的完整列表值。
spam = ['cat', 'bat', 'rat', 'elephant']
spam[-1]
'The ' + spam[-1] + ' is afraid of the ' + spam[-3] + '.'
正如索引可以从列表中获取单个值一样,切片可以从列表中以新列表的形式获取多个值。一个切片在方括号之间键入,就像索引一样,但是它有两个用冒号分隔的整数。注意索引和切片之间的区别。
spam[2]
spam[1:4]
在一个切片中,第一个整数是切片开始的索引。第二个整数是片段结束的索引。第二个索引上的值将上升到但不包括该值。切片评估为新的列表值。
spam = ['cat', 'bat', 'rat', 'elephant']
spam[1:3]
['bat', 'rat']
spam = ['cat', 'bat', 'rat', 'elephant']
spam[:2]
['cat', 'bat']
len()函数将返回传递给列表值的值的数量,就像可以计算字符串值中的字符数一样。
spam = ['cat', 'dog', 'moose']
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] = 12345
spam
['cat', 'aardvark', 'aardvark', 12345]
可以组合两个列表来创建一个新的列表值,方法是将两个字符串组合成一个新的字符串值。 *运算符也可以与列表和整数值一起使用以复制列表。
[1, 2, 3] + ['A', 'B', 'C']
[1, 2, 3, 'A', 'B', 'C']
['X', 'Y', 'Z'] * 3
['X', 'Y', 'Z', 'X', 'Y', 'Z', 'X', 'Y', 'Z']
spam = ['cat', 'bat', 'rat', 'elephant']
del spam[2]
spam
['cat', 'bat', 'elephant']
catName1 = 'ZhangYu'
catName2 = 'Pooka'
catName3 = 'Simon'
catName4 = 'Lady Macbeth'
catName5 = 'Fat-tail'
catName6 = 'Miss Cleo'
print('Enter the name of cat 1:')
catName1 = input()
print('Enter the name of cat 2:')
catName2 = input()
print('Enter the name of cat 3:')
catName3 = input()
print('Enter the name of cat 4:')
catName4 = input()
print('Enter the name of cat 5:')
catName5 = input()
print('Enter the name of cat 6:')
catName6 = input()
print('The cat names are:')
print(catName1 + ' ' + catName2 + ' ' + catName3 + ' ' + catName4 + ' ' +
catName5 + ' ' + catName6)
可以改成:
catNames = []
while True:
print('Enter the name of cat ' + str(len(catNames) + 1) +
' (Or enter nothing to stop.):')
name = input()
if name == '':
break
catNames = catNames + [name] # list concatenation
print('The cat names are:')
for name in catNames:
print(' ' + name)
当您运行此程序时,输出将如下所示:
Enter the name of cat 1 (Or enter nothing to stop.):
ZhangYu
Enter the name of cat 2 (Or enter nothing to stop.):
Pooka
Enter the name of cat 3 (Or enter nothing to stop.):
Simon
Enter the name of cat 4 (Or enter nothing to stop.):
Lady Macbeth
Enter the name of cat 5 (Or enter nothing to stop.):
Fat-tail
Enter the name of cat 6 (Or enter nothing to stop.):
Miss Cleo
Enter the name of cat 7 (Or enter nothing to stop.):The cat names are:
ZhangYu
Pooka
Simon
Lady Macbeth
Fat-tail
Miss Cleo
使用列表的好处是,您的数据现在处于结构中,因此您的程序在处理数据方面比使用多个重复变量更灵活。