Python之列表

2020-08-29  本文已影响0人  止于枯骨1

1.序列

2.列表(list)

2.1列表的概念

列表里边存储的数据我们称之为元素

lst = ['python',1,True,None,[2,3,4]]

2.2列表的使用

2.2.1基本操作

1.创建列表:通过 [] 创建列表

list1 = []
list2 = [1, 2, 3, 4]
print(list1, list2)
# ----------输出----------
# [] [1, 2, 3, 4]

2.元素的获取:再列表中可以通过索引(index)或者下标来获取列表中的元素
什么是索引?
答:索引是从0开始 ,列表的第一个位置的索引就是0 以此类推.

list1 = ['Sun WuKong', 'Zhu Bajie', 'Monk Sha', 'Tang Monk']
print(list1[0], list1[3])
# ----------输出----------
# Sun WuKong Tang Monk

如果打印 print(list1[5])
会出现这个错误" IndexError: list index out of range"
说明超出最大索引值就会报错

list1 = ['Sun WuKong', 'Zhu Bajie', 'Monk Sha', 'Tang Monk']
print(list1[-1], list1[-3])
# ----------输出----------
# Tang Monk Zhu Bajie
list1 = ['Sun WuKong', 'Zhu Bajie', 'Monk Sha', 'Tang Monk']
print(len(list1))
# ----------输出----------
# 4

2.2.2切片

hero = ['钢铁侠','绿巨人','蜘蛛侠','黑寡妇','蚁人','美国队长']
# 做切片操作时,总会返回一个新的列表,不会影响原来的列表
print(hero[0:1])   
print(hero[2:4])  
print(hero)      
#输出结果:
# ['钢铁侠']
 #['蜘蛛侠', '黑寡妇']
 #['钢铁侠', '绿巨人', '蜘蛛侠', '黑寡妇', '蚁人', '美国队长']
hero = ['钢铁侠','绿巨人','蜘蛛侠','黑寡妇','蚁人','美国队长']
print(hero[1:])   
print(hero[:3])  
print(hero[:])    

#输出结果
 #['绿巨人', '蜘蛛侠', '黑寡妇', '蚁人', '美国队长']
  #['钢铁侠', '绿巨人', '蜘蛛侠']
 #['钢铁侠', '绿巨人', '蜘蛛侠', '黑寡妇', '蚁人', '美国队长']

hero = ['钢铁侠','绿巨人','蜘蛛侠','黑寡妇','蚁人','美国队长']
# print(hero[1:])    #['绿巨人', '蜘蛛侠', '黑寡妇', '蚁人', '美国队长']
# print(hero[:3])    #['钢铁侠', '绿巨人', '蜘蛛侠']
# print(hero[:])     #['钢铁侠', '绿巨人', '蜘蛛侠', '黑寡妇', '蚁人', '美国队长']
print(hero[0:5:1])  # ['钢铁侠', '绿巨人', '蜘蛛侠', '黑寡妇', '蚁人']
print(hero[0:5:2]) #['钢铁侠', '蜘蛛侠', '蚁人']
print(hero[::0])  # 报错:ValueError: slice step cannot be zero 步长不能是0
print(hero[::-1])  # ['美国队长', '蚁人', '黑寡妇', '蜘蛛侠', '绿巨人', '钢铁侠']
print(hero[::-2])  # ['美国队长', '黑寡妇',  '绿巨人',]

3.通用操作

3.1 + 和 *

lst = [1,2,3] + [4,5,6] 
lst1 = [1,2,3] * 2       
lst2 = [4,5,6] * [1,2,3] 

#输出结果:
 [1, 2, 3, 4, 5, 6]
 [1,2,3,1,2,3]
报错 :TypeError: can't multiply sequence by non-int of type 'list'

3.2 in 和 not in

hero = ['钢铁侠','绿巨人','蜘蛛侠','黑寡妇','蚁人','美国队长']
print('蜘蛛侠'in hero)        
print('钢铁侠'not in hero)     

输出结果:
 True
 False

3.3 更多

lst = [1,2,3,4,5,6]
print(len(lst))

输出结果:
6
lst = [1,2,3,4,5,10,6]
print(max(lst),min(lst))

输出结果:
10 
1

#比较的是 ASCII 大小
list2 = ['Sun WuKong', 'Zhu Bajie', 'Monk Sha', 'Tang Monk']
print(max(list2), min(list2))
输出结果:
Zhu Bajie 
Monk Sha

hero =['钢铁侠','绿巨人','蜘蛛侠','黑寡妇','蚁人','美国队长','蜘蛛侠','美国队长','蜘蛛侠']
print(hero.index('蜘蛛侠'))
print(hero.index('蜘蛛侠',3))
print(hero.index('蜘蛛侠',3,7))
输出结果:
2
6
6

list1 = ['Monk Sha', 'Zhu Bajie', 'Monk Sha', 'Tang Monk']
print(list1.count('Monk Sha'))

输出结果:
2

4.列表的修改

hero =['钢铁侠', '绿巨人', '蜘蛛侠', '黑寡妇', '蚁人', '美国队长']
hero[0:1] = '雷神'
print(hero)   
输出结果:
 ['雷', '神', '绿巨人', '蜘蛛侠', '黑寡妇', '蚁人', '美国队长']

hero[0:1] = ['雷神']
print(hero)   
输出结果:
['雷神', '绿巨人', '蜘蛛侠', '黑寡妇', '蚁人', '美国队长']
hero =['钢铁侠', '绿巨人', '蜘蛛侠', '黑寡妇', '蚁人', '美国队长']
hero[0] = '雷神'
print(hero) 
输出结果:
['雷神', '绿巨人', '蜘蛛侠', '黑寡妇', '蚁人', '美国队长']
hero =['钢铁侠', '绿巨人', '蜘蛛侠', '黑寡妇', '蚁人', '美国队长']
hero[0:4:2] = '雷神'
print(hero)
输出结果:
['雷', '绿巨人', '神', '黑寡妇', '蚁人', '美国队长']


hero[0:4:2] = ['雷神','神奇女侠']
print(hero)
输出结果:
['雷神', '绿巨人', '神奇女侠', '黑寡妇', '蚁人', '美国队长']

print(hero[0:5:2]) #输出结果是:['钢铁侠', '蜘蛛侠', '蚁人']
hero[0:5:2] = '雷神'
输出结果:
报错:ValueError: attempt to assign sequence of size 2 to extended slice of size 3
hero =['钢铁侠', '绿巨人', '蜘蛛侠', '黑寡妇', '蚁人', '美国队长']
#向索引为0的位置插入元素
hero[0:0] = '雷神' 
print(hero)   
输出结果:
['雷神', '钢铁侠', '绿巨人', '蜘蛛侠', '黑寡妇', '蚁人', '美国队长']
#向索引为1的位置插入元素
hero[1:1] = '雷神' 
print(hero)   
输出结果:
['钢铁侠', '雷神', '绿巨人', '蜘蛛侠', '黑寡妇', '蚁人', '美国队长']
hero =['钢铁侠', '绿巨人', '蜘蛛侠', '黑寡妇', '蚁人', '美国队长']
hero[1:2]=[]
print(hero)
输出结果:
['钢铁侠', '蜘蛛侠', '黑寡妇', '蚁人', '美国队长']

#算是(替换删除)
hero[0:3]=['雷神']
print(hero)
输出结果:
['雷神', '黑寡妇', '蚁人', '美国队长']
hero =['钢铁侠', '绿巨人', '蜘蛛侠', '黑寡妇', '蚁人', '美国队长']
del hero[2]
print(hero)
输出结果:
['钢铁侠', '绿巨人', '黑寡妇', '蚁人', '美国队长']

hero =['钢铁侠', '绿巨人', '蜘蛛侠', '黑寡妇', '蚁人', '美国队长']
del hero[0:2]
print(hero)
输出结果:
['蜘蛛侠', '黑寡妇', '蚁人', '美国队长']

5.列表的方法

hero = ['钢铁侠','绿巨人','蜘蛛侠']
hero.append('黑寡妇')
print(hero)

输出结果:
['钢铁侠', '绿巨人', '蜘蛛侠', '黑寡妇']
hero = ['钢铁侠','绿巨人','蜘蛛侠']
hero.insert(2,'黑寡妇')
print(hero)

输出结果:
['钢铁侠', '绿巨人', '黑寡妇', '蜘蛛侠']
hero = ['钢铁侠','绿巨人','蜘蛛侠']
hero.extend(['黑寡妇','超人'])
print(hero)

输出结果:
['钢铁侠', '绿巨人', '蜘蛛侠', '黑寡妇', '超人']
hero = ['钢铁侠','绿巨人','蜘蛛侠']
result= hero.pop(1)
print(result)
print(hero)
输出结果:
绿巨人
['钢铁侠', '蜘蛛侠']
hero = ['钢铁侠','绿巨人','蜘蛛侠']
hero.remove('绿巨人')
print(hero)
输出结果:
['钢铁侠', '蜘蛛侠']
hero = ['钢铁侠','绿巨人','蜘蛛侠']
hero.reverse()
print(hero)
输出结果:
['蜘蛛侠', '绿巨人', '钢铁侠']
lst = list('qwerasd')
lst.sort()
print(lst)
输出结果:
['a', 'd', 'e', 'q', 'r', 's', 'w']

lst.sort(reverse=True)
print(lst)
输出结果:
['w', 's', 'r', 'q', 'e', 'd', 'a']


6.遍历列表

6.1for循环

语法:
for 变量 in 序列:
        代码块

hero = ['钢铁侠','绿巨人','蜘蛛侠','黑寡妇']
for a in hero:
    print(a)

输出结果:
钢铁侠
绿巨人
蜘蛛侠
黑寡妇

6.2 range(start, stop[, step])

range() 函数 可以用来生成一个自然数的序列

a = range(5)
print(a)
print(list(a))

输出结果:
range(0, 5)
[0, 1, 2, 3, 4]

***************************
for i in range(5):
    print('python')

输出结果:
python
python
python
python
python

*****************************
print(list(range(0,5,2)))
输出结果:
[0, 2, 4]
上一篇 下一篇

猜你喜欢

热点阅读