a = ['java', 'python', 'javascript', 'c', 'c++', 'php', 'css', 'html5', 'go']
print(a[1:4])
['python', 'javascript', 'c']
表示从索引1开始到索引4结束,但是不包括索引4。
a = ['java', 'python', 'javascript', 'c', 'c++', 'php', 'css', 'html5', 'go']
print(a[:4])
['java', 'python', 'javascript', 'c']
表示从索引0开始到索引4结束。 但是不包括索引4。
a = ['java', 'python', 'javascript', 'c', 'c++', 'php', 'css', 'html5', 'go']
print(a[-4:])
['php', 'css', 'html5', 'go']
表示从索引-4开始到结束
a = ['java', 'python', 'javascript', 'c', 'c++', 'php', 'css', 'html5', 'go']
print(a[1:6:2])
['python', 'c', 'php']
表示索引1到索引6之间每隔两个元素取一个
示例5
a = ['java', 'python', 'javascript', 'c', 'c++', 'php', 'css', 'html5', 'go']
print(a[::])
['java', 'python', 'javascript', 'c', 'c++', 'php', 'css', 'html5', 'go']
取整个列表
说明:切片可用于列表。 元组。 字符串。
示例1
迭代列表
a = ['java', 'python', 'javascript', 'c', 'c++', 'php', 'css', 'html5', 'go']
for key in a:
print(key)
java
python
javascript
c
c++
php
css
html5
go
迭代字典的key
b = {
"name": "张晓明",
"age": 23,
"city": "成都市"
}
for key in b:
print(key)
name
age
city
迭代字典的value
b = {
"name": "张晓明",
"age": 23,
"city": "成都市"
}
for value in b.values():
print(value)
张晓明
23
成都市
迭代字典的key和value
b = {
"name": "张晓明",
"age": 23,
"city": "成都市"
}
for item in b.items():
print(item)
('name', '张晓明')
('age', 23)
('city', '成都市')
注意。因为dict的存储不是按照list的方式顺序排列,迭代出的结果顺序可能不一样。
迭代字符串
b = 'python'
for key in b:
print(key)
p
y
t
h
o
n
迭代元组
a = ('java', 'python', 'javascript', 'c', 'c++', 'php', 'css', 'html5', 'go')
for key in a:
print(key)
java
python
javascript
c
c++
php
css
html5
go
collections.abc模块的Iterable类型判断对象是否可以被迭代。
from collections.abc import Iterable
a = ('java', 'python', 'javascript', 'c', 'c++', 'php', 'css', 'html5', 'go')
print(isinstance(a, Iterable))
print(isinstance("python", Iterable))
print(isinstance(123, Iterable))
True
True
False
通过enumerate函数获得下标
a = ('java', 'python', 'javascript', 'c', 'c++', 'php', 'css', 'html5', 'go')
for index, value in enumerate(a):
print(index, value)
0 java
1 python
2 javascript
3 c
4 c++
5 php
6 css
7 html5
8 go
生成1-10的列表
a = list(range(1, 11))
print(a)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
生成如1,4,9,16一样的列表
a = [x * x for x in range(1, 11)]
print(a)
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
生成1,4,6,8一样的列表
a = [x * 2 for x in range(1, 11)]
print(a)
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
加上条件生成列表
a = [x * x for x in range(1, 11) if x % 2 == 0]
print(a)
[4, 16, 36, 64, 100]
双层循环生成列表
a = [x * n for x in range(1, 6) for n in range(2, 7)]
print(a)
[2, 3, 4, 5, 6, 4, 6, 8, 10, 12, 6, 9, 12, 15, 18, 8, 12, 16, 20, 24, 10, 15, 20, 25, 30]
双层循环家条件生成列表
a = [x * n for x in range(1, 6) for n in range(2, 7) if x % 2 == 0]
print(a)
[4, 6, 8, 10, 12, 8, 12, 16, 20, 24]
两个变量生成列表
a = [x + y for x, y in enumerate(range(1, 6))]
print(a)
[1, 3, 5, 7, 9]
if语句在for语句后时不能加else。 因为此时if语句是一个筛选条件
a = [x + y for x, y in enumerate(range(1, 6)) if x % 2 == 0]
print(a)
[1, 5, 9]
a = [x + y for x, y in enumerate(range(1, 6)) if x % 2 == 0 else y % 2 == 0]
print(a)
a = [x + y for x, y in enumerate(range(1, 6)) if x % 2 == 0 else y % 2 == 0]
SyntaxError: invalid syntax
if
写在for
前面必须加else
,否则报错:这是因为for
前面的部分是一个表达式,它的出的是一个结果a = [x if x % 2 == 0 else "python" for x in range(1, 50)]
print(a)
['python', 2, 'python', 4, 'python', 6, 'python', 8, 'python', 10, 'python', 12, 'python', 14, 'python', 16, 'python', 18, 'python', 20, 'python', 22, 'python', 24, 'python', 26, 'python', 28, 'python', 30, 'python', 32, 'python', 34, 'python', 36, 'python', 38, 'python', 40, 'python', 42, 'python', 44, 'python', 46, 'python', 48, 'python']
---------------------------
a = [x if x % 2 == 0 for x in range(1, 50)]
print(a)
a = [x if x % 2 == 0 for x in range(1, 50)]
^
SyntaxError: invalid syntax
-----------------------------
a = [x * y if x % 2 == 0 else "python" for x, y in enumerate(range(1, 50))]
print(a)
[0, 'python', 6, 'python', 20, 'python', 42, 'python', 72, 'python', 110, 'python', 156, 'python', 210, 'python', 272, 'python', 342, 'python', 420, 'python', 506, 'python', 600, 'python', 702, 'python', 812, 'python', 930, 'python', 1056, 'python', 1190, 'python', 1332, 'python', 1482, 'python', 1640, 'python', 1806, 'python', 1980, 'python', 2162, 'python', 2352]