序列(索引)
- Python最基本的数据结构是序列(sequence),序列里每个元素被分配一个序号,也称为索引(index),从0开始。
一、访问列表元素
0 | 1 | 2 | 3 | 4 | 5 | 6 |
---|---|---|---|---|---|---|
0 | 1 | 2 | … | … | len(week)-2 | len(week)-1 |
Monday | Tuesday | Wednesday | Thursday | Friday | Saturday | Sunday |
-(len(week)) | -(len(week)-1) | -(len(week)-2) | … | … | -2 | -1 |
-7 | -6 | -5 | -4 | -3 | -2 | -1 |
1、 访问列表元素
(1)正向遍历
week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
for i in range(len(week)):
print(week[i], end=' ')
输出结果:
Monday Tuesday Wednesday Thursday Friday Saturday Sunday
(2)反向遍历
week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
for i in range(-1, -(len(week) + 1), -1):
print(week[i], end=' ')
输出结果:
Sunday Saturday Friday Thursday Wednesday Tuesday Monday
二、标准类型运算符
1、 值比较
In [5]:a = 'python'
In [6]:b = 'java'
In [7]:a == b
Out[7]: False
In [8]:a != b
Out[8]: True
In [9]:a > b # p的ASCII码:112;j的ASCII码:106
Out[9]: True
In [10]:a < b
Out[10]: False
2、 对象身份比较
In [5]:a = 'python'
In [6]:b = 'java'
In [14]:a is b
Out[14]: False
In [15]:a is not b
Out[15]: True
3、 布尔运算
In [5]:a = 'python'
In [6]:b = 'java'
In [16]: a and b # and 取后一个
Out[16]: 'java'
In [17]: b and a
Out[17]: 'python'
In [18]: a or b # or 取前一个
Out[18]: 'python'
In [19]: b or a
Out[19]: 'java'
In [20]: not a
Out[20]: False
In [21]: a not b
File "<ipython-input-20-fda1ac746f0e>", line 1
a not b
^
SyntaxError: invalid syntax
三、 序列类型运算符
- 切片运算:
str[start:end:step]
1、 针对字符串
In [1]:word = 'international'
In [2]:word[4], word[-8], word[5 - len(word)]
Out[2]: ('r', 'n', 'n')
In [4]:word[2:8]
Out[4]: 'ternat'
In [5]:word[2:8:2]
Out[5]: 'tra'
In [6]:word[::]
Out[6]: 'international'
In [7]:word[::-1]
Out[7]: 'lanoitanretni'
In [8]:word * 3
Out[8]: 'internationalinternationalinternational'
In [9]:word + ' view'
Out[9]: 'international view'
In [10]:'inter' in word
Out[10]: True
In [11]:'inter' not in word
Out[11]: False
2、 针对列表
In [1]:names = ['天子', '宽窄', '利群', '熊猫']
In [2]:names[2], names[-3], names[2 - len(names)]
Out[2]: ('利群', '宽窄', '利群')
In [4]:names[1:4]
Out[4]: ['宽窄', '利群', '熊猫']
In [5]:names[1:4:2]
Out[5]: ['宽窄', '熊猫']
In [6]:names[::]
Out[6]: ['天子', '宽窄', '利群', '熊猫']
In [8]:names[::-1]
Out[8]: ['熊猫', '利群', '宽窄', '天子']
In [9]:names * 3
Out[9]: ['天子', '宽窄', '利群', '熊猫', '天子', '宽窄', '利群', '熊猫', '天子', '宽窄', '利群', '熊猫']
In [10]:names + ['中华', '荷花']
Out[10]: ['天子', '宽窄', '利群', '熊猫', '中华', '荷花']
In [11]:'宽窄' in names
Out[11]: True
In [12]:'煊赫门' in names
Out[12]: False
四、 序列内建函数
1、 类型转换
In [13]:str(666), str(True)
Out[13]: ('666', 'True')
In [18]:list('internet')
Out[18]: ['i', 'n', 't', 'e', 'r', 'n', 'e', 't']
In [19]:tuple('internet')
Out[19]: ('i', 'n', 't', 'e', 'r', 'n', 'e', 't')
2、 序列类型可用内建函数
(1)长度函数 - len()
In [1]:word = 'internet'
In [2]:scores = [56, 78, 34, 70]
In [3]:student = ('001', '明明', '男')
In [4]:cities = {1: '北京', 2: '上海'}
In [5]:len(word)
Out[5]: 8
In [6]:len(scores)
Out[6]: 4
In [8]:len(student)
Out[8]: 3
In [9]:len(cities)
Out[9]: 2
(2)求和、最大值与最小值函数
In [10]:scores = [45, 67, 89, 65]
In [11]:sum(scores)
Out[11]: 266
In [12]:sum(scores) / len(scores)
Out[12]: 66.5
In [13]:max(scores)
Out[13]: 89
In [14]:min(scores)
Out[14]: 45
(3) 链接函数 - zip()
ids = [1, 2, 3, 4]
cities = ['北京', '上海', '广州', '深圳']
city_list = zip(ids, cities)
for city in city_list:
print(city)
输出结果:
(1, '北京')
(2, '上海')
(3, '广州')
(4, '深圳')
(4)排序函数 - sorted()
In [17]:word = 'flower'
In [18]:sorted(word) # 升序
Out[18]: ['e', 'f', 'l', 'o', 'r', 'w']
In [19]:sorted(word, reverse = True) # “reverse = True” 降序
Out[19]: ['w', 'r', 'o', 'l', 'f', 'e']
In [20]:scores = [45, 67, 89, 65]
In [21]:sorted(scores) # 升序
Out[21]: [45, 65, 67, 89]
In [22]:sorted(scores, reverse = True) # “reverse = True” 降序
Out[22]: [89, 67, 65, 45]
(5)反序函数 - reversed()
word = 'flower'
cities = ['北京', '上海', '广州', '深圳']
print(reversed(word))
for c in reversed(word):
print(c, end=" ")
for city in reversed(cities):
print(city, end=" ")
输出结果:
<reversed object at 0x000001F202EEA7F0> # 输出的是内存地址
r e w o l f
深圳 广州 上海 北京
(6)枚举函数 - enumerate()
In [20]:word = 'internet'
...:for i in enumerate(word):
...: print(i)
(0, 'i')
(1, 'n')
(2, 't')
(3, 'e')
(4, 'r')
(5, 'n')
(6, 'e')
(7, 't')
In [21]:word = 'internet'
...:for i, c in enumerate(word):
...: print('{}:{}'.format(i, c))
0:i
1:n
2:t
3:e
4:r
5:n
6:e
7:t
In [1]:names = ['红塔山', '云烟', '玉溪', '红河', '大重九']
...:for i, c in enumerate(names):
...: print('{}:{}'.format(i, c))
0:红塔山
1:云烟
2:玉溪
3:红河
4:大重九