Python_list(四)数字列表和切片

/ Python / 没有评论 / 1017浏览

生成数字列表 range()

[root@pa1 lijinghua]#python number.py
1
2
3
4
[root@pa1 lijinghua]#cat number.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
for number in range(1,5):
	print(number)
[root@pa1 lijinghua]#python number.py
1
2
3
4
5
[root@pa1 lijinghua]#cat number.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
for number in range(1,6):
	print(number)

使用range()创建数字列表

[root@pa1 lijinghua]#python range.py
[1, 2, 3, 4, 5]
[root@pa1 lijinghua]#cat range.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
number=list(range(1,6))
print (number)

打印出1~10内的偶数

格式 (起始数+终值+不断相加的数字) 在这个示例中,函数range()从2开始,然后不断的加2,直到达到或超过终值(11)

[root@pa1 lijinghua]#python even_number.py
[2, 4, 6, 8, 10]
[root@pa1 lijinghua]#cat even_number.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
even_number=list(range(2,11,2))
print(even_number)

平方表示 (**2)

[root@pa1 lijinghua]#python squares.py
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
[root@pa1 lijinghua]#cat squares.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
squares=[]
for value in range(1,11):                       #for循环从range()函数取值
	square=value**2                                 #平方下
	squares.append(square)                   #追加到列表中
print(squares)                                          #打印最终的列表
[root@pa1 lijinghua]#python  squares_1.py
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
[root@pa1 lijinghua]#cat squares_1.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
squares=[]
for value in range(1,11):                       #for循环从range()函数取值
	squares.append(value**2)                   #直接追加到最终要打印的列表中
print(squares) 

列表解析出场了

[root@pa1 lijinghua]#python squares_2.py
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
[root@pa1 lijinghua]#cat squares_2.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
squares=[value**2 for value in range(1,11)]   #注意这里的for循环没有冒号
print(squares)

对数字执行简单的统计计算 min() max() sum()

示范

>>> digits=[1,2,3,4,5,6,7,8,9]
>>> sum(digits)
45
>>> min(digits)
1
>>> max(digits)
9

注意: 可能以上只适用于百万个数字的列表


切片

与函数range()一样,Python在到达你指定的第二个索引前面的元素后停止,要输出列表中的前三个元素,需要制定索引0~3

示范

>>> player=['lili','congcong','ytuxi','jinghua']
>>> print(player[0:3])
['lili', 'congcong', 'ytuxi']

示范

>>> player=['lili','congcong','ytuxi','jinghua']
>>> print(player[1:4])                          #请仔细领略齐总的微妙
['congcong', 'ytuxi', 'jinghua']

示范

>>> player=['lili','congcong','ytuxi','jinghua']
>>> print(player[:4])
['lili', 'congcong', 'ytuxi', 'jinghua']

示范

>>> player=['lili','congcong','ytuxi','jinghua']
>>> print(player[2:])
['ytuxi', 'jinghua']

示范

>>> player=['lili','congcong','ytuxi','jinghua']
>>> print(player[-3:])
['congcong', 'ytuxi', 'jinghua']

遍历切片

遍历切片 示范

[root@pa1 lijinghua]#python qiepian.py 
they are my frinde:
Lili
Congcong
Yuxi
[root@pa1 lijinghua]#cat qiepian.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
names=['lili','congcong','yuxi','jinghua']
print("they are my frinde:")
for name in names[:3]:         #取出前三个元素
	print(name.title())

复制切片[:]

示范

>>> names=['lili','congcong','yuxi','jinghua']
>>> name=names[:]
>>> print(name)
['lili', 'congcong', 'yuxi', 'jinghua']
>>> print(names)
['lili', 'congcong', 'yuxi', 'jinghua']