[关闭]
@zhangyu756897669 2017-08-23T15:28:55.000000Z 字数 3763 阅读 597

python 官方文档10

python官方文档


列表,索引,切片和list()函数

  1. 在开始编写程序之前,您需要了解的另一个主题是列表数据类型及其小弟元组。列表和元组可以包含多个值,这样可以更容易地编写处理大量数据的程序。由于列表本身可以包含其他列表,您可以使用它们将数据排列成层次结构。
  2. 在本章中,我将讨论列表的基础知识。我还会教你关于某些数据类型的值的函数的方法。然后我将简要介绍列表式的元组和字符串数据类型,以及它们与列表值的比较。在下一章中,我将向您介绍字典数据类型。

列表数据类型

  1. 列表是在有序序列中包含多个值的值。术语列表值是指列表本身(它是可以存储在变量中的值,或者像任何其他值一样传递给函数),而不是列表值中的值。
  2. 列表值如下所示:['cat','bat','rat','elephant']。正如使用引号字符键入字符串值来标记字符串开始和结束的位置一样,一个列表以一个开口方括号开头,以一个闭合方括号[]结尾。列表中的值也称为项目。项目用逗号分隔(即它们以逗号分隔)。例如:
  1. [1, 2, 3]
  2. ['cat', 'bat', 'rat', 'elephant']
  3. ['hello', 3.1415, True, None, 42]
  4. spam = ['cat', 'bat', 'rat', 'elephant']
  5. spam

在索引列表中获取单个值

例如:

  1. spam = ['cat', 'bat', 'rat', 'elephant']
  2. spam[0]
  3. ```
  4. ```
  5. spam[1]
  1. spam[2]
  1. spam[3]
  1. ['cat', 'bat', 'rat', 'elephant'][3]
  1. 'Hello ' + spam[0]
  1. 'The ' + spam[1] + ' ate the ' + spam[0] + '.'
  1. spam = ['cat', 'bat', 'rat', 'elephant']
  2. spam[1000]

Traceback (most recent call last):
File "", line 1, in
spam[10000]
IndexError: list index out of range

  1. spam = ['cat', 'bat', 'rat', 'elephant']
  1. spam[1]
  1. spam[1.0]
  1. spam[int(1.0)]
  1. spam = [['cat', 'bat'], [10, 20, 30, 40, 50]]
  1. spam[0]
  1. spam[0][1]
  1. spam[1][4]

第一个索引指示要使用的列表值,第二个索引指示列表值中的值。例如,spam 0打印“bat”,第一个列表中的第二个值。如果只使用一个索引,程序将打印该索引的完整列表值。

负指数

  1. spam = ['cat', 'bat', 'rat', 'elephant']
  2. spam[-1]
  1. 'The ' + spam[-1] + ' is afraid of the ' + spam[-3] + '.'

获取带有切片的子列表

  1. spam = ['cat', 'bat', 'rat', 'elephant']
  2. spam[1:3]

['bat', 'rat']

  1. spam = ['cat', 'bat', 'rat', 'elephant']
  2. spam[:2]

['cat', 'bat']

用len()获取列表的长度

len()函数将返回传递给列表值的值的数量,就像可以计算字符串值中的字符数一样。

  1. spam = ['cat', 'dog', 'moose']
  2. len(spam)

3

使用索引更改列表中的值

  1. spam = ['cat', 'bat', 'rat', 'elephant']
  2. spam[1] = 'aardvark'
  3. spam

['cat', 'aardvark', 'rat', 'elephant']

  1. spam[2] = spam[1]
  2. spam

['cat', 'aardvark', 'aardvark', 'elephant']

  1. spam[-1] = 12345
  2. spam

['cat', 'aardvark', 'aardvark', 12345]

列表连接和列表复制

可以组合两个列表来创建一个新的列表值,方法是将两个字符串组合成一个新的字符串值。 *运算符也可以与列表和整数值一起使用以复制列表。

  1. [1, 2, 3] + ['A', 'B', 'C']

[1, 2, 3, 'A', 'B', 'C']

  1. ['X', 'Y', 'Z'] * 3

['X', 'Y', 'Z', 'X', 'Y', 'Z', 'X', 'Y', 'Z']

用del语句列表中删除值

  1. spam = ['cat', 'bat', 'rat', 'elephant']
  2. del spam[2]
  3. spam

['cat', 'bat', 'elephant']

使用列表

  1. catName1 = 'ZhangYu'
  2. catName2 = 'Pooka'
  3. catName3 = 'Simon'
  4. catName4 = 'Lady Macbeth'
  5. catName5 = 'Fat-tail'
  6. catName6 = 'Miss Cleo'
  1. print('Enter the name of cat 1:')
  2. catName1 = input()
  3. print('Enter the name of cat 2:')
  4. catName2 = input()
  5. print('Enter the name of cat 3:')
  6. catName3 = input()
  7. print('Enter the name of cat 4:')
  8. catName4 = input()
  9. print('Enter the name of cat 5:')
  10. catName5 = input()
  11. print('Enter the name of cat 6:')
  12. catName6 = input()
  13. print('The cat names are:')
  14. print(catName1 + ' ' + catName2 + ' ' + catName3 + ' ' + catName4 + ' ' +
  15. catName5 + ' ' + catName6)

可以改成:

  1. catNames = []
  2. while True:
  3. print('Enter the name of cat ' + str(len(catNames) + 1) +
  4. ' (Or enter nothing to stop.):')
  5. name = input()
  6. if name == '':
  7. break
  8. catNames = catNames + [name] # list concatenation
  9. print('The cat names are:')
  10. for name in catNames:
  11. 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

使用列表的好处是,您的数据现在处于结构中,因此您的程序在处理数据方面比使用多个重复变量更灵活。

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注