2022年 11月 3日

《Python基础教程(第2版·修订版)》 第2章 列表和序列(学习笔记·二)

2.2.2分片

1.索引用于访问单个元素,可以使用分片操作来访问一定范围内的元素。

  1. >>> tag = '<a herf="http://www.python.org"> Python web site</a>'
  2. >>> tag[9:30]
  3. 'http://www.python.org'
  4. >>> tag[32:-4]
  5. ' Python web site'
  1. >>> numbers = [1,2,3,4,5,6,7,8,9,10]
  2. >>> numbers[4:6]
  3. [5, 6]
  4. >>> numbers[0:1]
  5. [1]
  1. >>> numbers[7:10]#访问后三个元素
  2. [8, 9, 10]
  3. >>> numbers[-3:-1]
  4. [8, 9]
  5. >>> numbers[-3:]
  6. [8, 9, 10]
  7. >>> numbers[-3:0]#会发生错误
  8. []
  9. >>> numbers[:]
  10. [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  11. >>>

2_2.py  分片示例

  1. #对http://www.something.com形式的URL进行分割
  2. url = raw_input('Please enter the URL: ')
  3. domain = url[11:-4]
  4. print "Domain name: " + domain

运行效果:

  1. >>>
  2. Please enter the URL: http://www.python.org
  3. Domain name: python

2.更大的步长

运行示例:

  1. >>> numbers = [1,2,3,4,5,6,7,8,9,10]
  2. >>> numbers[0:10:1]
  3. [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  4. >>> numbers[0:10:2]#前两个构成范围,最后一个是步长
  5. [1, 3, 5, 7, 9]
  6. >>> numbers[3:6:3]
  7. [4]
  8. >>> numbers[::4]
  9. [1, 5, 9]
  10. >>> numbers[8:3:-1]#步长可以为负数,但不能为0,表示从右往左提取元素
  11. [9, 8, 7, 6, 5]
  12. >>>

2.2.3序列相加

  1. >>> [1,2,3] + [4,5,6]
  2. [1, 2, 3, 4, 5, 6]
  3. >>> 'Hello,' + 'world!'
  4. 'Hello,world!'

2.2.4乘法

运行示例:

  1. >>> 'python' * 5
  2. 'pythonpythonpythonpythonpython'
  3. >>> [42] * 10
  4. [42, 42, 42, 42, 42, 42, 42, 42, 42, 42]

None是一个Python的内建值,它的确切含意是“这里什么也没有”。如下例子可以初始化一个长度为10的空列表

  1. >>> sequence = [None] * 10
  2. >>> sequence
  3. [None, None, None, None, None, None, None, None, None, None]

2_3 序列(字符串)乘法示例

  1. #以正确的宽度在居中的“盒子”内打印一个句子
  2. #注意,整数除法运算符(//)只能用在Python 2.2以及后续版本,在之前的版本中,只是用普通除法(/)
  3. # -*- coding: cp936 -*-
  4. sentence = raw_input("Sentence: ")
  5. screen_width = 80
  6. text_width = len(sentence)
  7. box_width = text_width + 6
  8. left_margin = (screen_width - box_width) // 2
  9. print
  10. print ' ' * left_margin + '+' + '-' * (box_width - 2) + '+'
  11. print ' ' * left_margin + '|' + ' ' * text_width + '|'
  12. print ' ' * left_margin + '|' + sentence + '|'
  13. print ' ' * left_margin + '|' + ' ' * text_width + '|'
  14. print ' ' * left_margin + '+' + '-' * (box_width - 2) + '+'
  15. print

运行效果

  1. >>>
  2. Sentence: He's a very naughty boy!
  3. +----------------------------+
  4. | |
  5. |He's a very naughty boy!|
  6. | |
  7. +----------------------------+

2.2.5 成员资格

这里用到一个 “in”运算符来检查成员资格,看示例

  1. >>> permissions = 'rw'
  2. >>> 'w' in permissions
  3. True
  4. >>> 'x' in permissions
  5. False
  6. >>> users = ['mlh','foo','bar']
  7. >>> raw_input('Enter your user name: ') in users
  8. Enter your user name: mlh
  9. True
  10. >>> subject = '$$$ Get rich now!!! $$$'
  11. >>> '$$$' in subject
  12. True
  13. >>>

2_4.py 序列成员资格示例

  1. #检查用户名和PIN码
  2. # -*- coding: cp936 -*-
  3. database = [
  4. ['albert','1234'],
  5. ['dilbert','4242'],
  6. ['smith','7524'],
  7. ['jones','9843']
  8. ]
  9. username = raw_input('User name: ')
  10. pin = raw_input('PIN code: ')
  11. if [username, pin] in database: print 'Access granted'

运行效果:

  1. >>>
  2. User name: albert
  3. PIN code: 1234
  4. Access granted

2.2.6 长度,最小值和最大值

  1. >>> numbers = [100,34,678]
  2. >>> len(numbers)
  3. 3
  4. >>> max(numbers)
  5. 678
  6. >>> min(numbers)
  7. 34
  8. >>> max(2,3)
  9. 3
  10. >>> min(9,3,2,5)
  11. 2