|
|
|
|
@ -0,0 +1,123 @@
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
"""
|
|
|
|
|
Created on Mon Oct 4 23:08:22 2021
|
|
|
|
|
|
|
|
|
|
@author: Administrator
|
|
|
|
|
"""
|
|
|
|
|
import math as ma
|
|
|
|
|
|
|
|
|
|
#变量可以储存一个元素,列表可以储存很多元素,方便整体操作
|
|
|
|
|
lst1=["hello","world",98,"hello"]#使用中括号创建列表
|
|
|
|
|
print(lst1)
|
|
|
|
|
lst2=list(["hello","world",98])#使用list()函数创建
|
|
|
|
|
print(lst2)
|
|
|
|
|
print(id(lst2))
|
|
|
|
|
print(type(lst2))
|
|
|
|
|
|
|
|
|
|
#列表有索引,左边从0开始计算,右边-1开始
|
|
|
|
|
print(lst1[0],lst1[-1])
|
|
|
|
|
print(lst1.index(98))#.index可以查找元素的索引
|
|
|
|
|
print(lst1.index("hello"))#重复元素只输出第一个
|
|
|
|
|
print(lst1.index("hello",1,4))#也可以指定查找范围,含头不含尾
|
|
|
|
|
|
|
|
|
|
#获取指定元素
|
|
|
|
|
lst3=["hello","world",98,"hello","world",234]
|
|
|
|
|
print(lst3[2])#获取索引为2的元素
|
|
|
|
|
a=lst3.count("hello")#将列表中指定元素的出现次数赋值给a
|
|
|
|
|
print(a)
|
|
|
|
|
|
|
|
|
|
#切片[start:stop:step],步长默认位一,和字符串差不多
|
|
|
|
|
lst4=lst3[1:5]#切出来的是一个新的列表对象,含头不含尾
|
|
|
|
|
print(lst4)
|
|
|
|
|
lst5=lst3[::-1]#开始和结束均采用默认
|
|
|
|
|
print(lst5)
|
|
|
|
|
|
|
|
|
|
#列表元素的判断和遍历
|
|
|
|
|
print("hello" in lst3)
|
|
|
|
|
print(99 not in lst3)
|
|
|
|
|
for item in lst3:#将lst3中的元素依次取出赋给item
|
|
|
|
|
print(item)
|
|
|
|
|
|
|
|
|
|
#元素的添加,没有添加新列表对象,会自动覆盖原列表
|
|
|
|
|
a=lst3.append(100)#append向列表末尾添加一个元素
|
|
|
|
|
print(a)#输出None
|
|
|
|
|
print(lst3)
|
|
|
|
|
lst3.append(lst1)#将lst1整体作为一个元素添加
|
|
|
|
|
print(lst3)
|
|
|
|
|
b=lst3[-1][1]#嵌套列表
|
|
|
|
|
print(b)
|
|
|
|
|
lst3.extend(lst1)#extend向列表末尾一次添加多个元素,此时lst1不再作为整体
|
|
|
|
|
print(lst3)
|
|
|
|
|
lst6=[10,20,30,40,50]
|
|
|
|
|
lst6.insert(0, 0)#insert(索引index,元素object)可以在任意位置插入一个元素
|
|
|
|
|
print(lst6)
|
|
|
|
|
#在任意位置添加多个元素:切片可以实现
|
|
|
|
|
lst3[1:3]=lst6#吧lst3中1到3(含头不含尾)切掉,替换成lst6
|
|
|
|
|
print(lst3)
|
|
|
|
|
|
|
|
|
|
#元素的删除
|
|
|
|
|
lst7=[0,10,20,30,40,50,20]
|
|
|
|
|
a=lst7.remove(20)#删掉指定元素,当存在重复元素时仅删掉第一个
|
|
|
|
|
print(a)#输出None
|
|
|
|
|
print(lst7)
|
|
|
|
|
lst8=[0,10,20,30,40,50,60,70]
|
|
|
|
|
a=lst8.pop(1)# 删除index索引为1的元素
|
|
|
|
|
print(a)#a为删除的元素
|
|
|
|
|
print(lst8)
|
|
|
|
|
#当然,还可以直接切片,会得到新的列表
|
|
|
|
|
lst9=lst8[1:6:2]
|
|
|
|
|
print(lst9)
|
|
|
|
|
lst8.clear()
|
|
|
|
|
print(lst8)
|
|
|
|
|
#del lst用于删除整个列表对象,原赋值不再存在
|
|
|
|
|
|
|
|
|
|
#列表元素的修改操作
|
|
|
|
|
lst9=[0,10,20,30,40,50,60,70]
|
|
|
|
|
lst9[1]=5#lst[index]=把原列表指定索引处更换成另一元素
|
|
|
|
|
print(lst9)
|
|
|
|
|
#在任意位置修改多个元素:见上文
|
|
|
|
|
#在任意位置添加多个元素:切片可以实现
|
|
|
|
|
lst3[1:3]=lst6#把lst3中1到3(含头不含尾)切掉,替换成lst6
|
|
|
|
|
print(lst3)
|
|
|
|
|
|
|
|
|
|
#列表的排序
|
|
|
|
|
lst10=[20,0,39,40,27,58,41,49,4,9,17]
|
|
|
|
|
lst10.sort()
|
|
|
|
|
print(lst10)#默认升序
|
|
|
|
|
lst10.sort(reverse=True)#改为降序
|
|
|
|
|
print(lst10)
|
|
|
|
|
lst11=sorted(lst10)#sorted函数产生一个新列表,也是默认升序
|
|
|
|
|
#lst11=sorted(lst10,reverse=True)
|
|
|
|
|
print(lst11)
|
|
|
|
|
|
|
|
|
|
#列表生成式
|
|
|
|
|
lst12=[i for i in range(1,10)]#利用for in语句生成列表,前面的值才是列表最终存储的
|
|
|
|
|
print(lst12)
|
|
|
|
|
lst13=[eval(format(ma.sqrt(i),".3f")) for i in range(1,10)]#format()内为str对象
|
|
|
|
|
print(lst13)
|
|
|
|
|
|
|
|
|
|
a=['a', 'b', 'c']
|
|
|
|
|
n=[1, 2, 3]
|
|
|
|
|
x=[a, n]
|
|
|
|
|
print(x)#[['a', 'b', 'c'], [1, 2, 3]]
|
|
|
|
|
print(x[0])#['a', 'b', 'c']
|
|
|
|
|
print(x[0][1])#'b'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|