列表
#使用[]表示列表,并使用","号分隔其中元素
bicycles=['trek','cannondale','redline','specialize']
print(bicycles)
#3.1.1 访问列表元素
#列表是有序集合 按照先指定是那个列表 再指定这个列表的要访问元素的位置
print(bicycles[0]," 访问bicycles列表的第0个元素")
print(bicycles[0].title()," bicyclies列表存储的是string类型,因此元素可以调用str的函数方法,进行一定的操作")
#3.1.2 索引是从0开始而不是1开始
print(bicycles[3],"因为索引是从0开始,所以正确的传参是要访问位置-1,4号位的索引是4-1=3")
#python为访问最后一个元素提供了特殊语法 -1
print(bicycles[-1],"-1位置为直接访问末尾元素")
#3.1.3使用列表中的各个值
message=f"My first bicycle was a {bicycles[1].upper()}"
print(message)
#Test
names=["rhp",'wyl','zsy','hzx','wq']
for name in names:
print(name)
message=" :how are you?"
for name in names:
message=name+message
print(message)
words=["骑自行车","坐汽车","轮渡","火car"]
types=['bicycle','car',"ship",'train']
for type in types:
if(type=="bicycle"):
print(f"{words[0]} and ",type)
if(type=="car"):
print(f"{words[1]} and",type)
if(type=='ship'):
print(f"{words[2]} and ",type)
if(type=='train'):
print(f"{words[-1]} and ",type)
修改、添加、删除元素
#列表是动态的
#3.2.1修改列表元素 指定那个列表——》指定元素为位置——》赋新值
motorcycles=['honda','yamaha','suzuki']
print(motorcycles)
motorcycles[0]="ducati"
print(motorcycles,"将1号位重新修改后的列表")
#3.2.2在列表中添加元素
#在列表末尾添加 直接使用append方法进行添加
motorcycles.append("ducati")
print(motorcycles)
#在列表中插入元素 直接使用insert方法 需要指定新元素和索引
motorcycles.insert(0,"honda")
print(motorcycles,"使用insert将hond添加到0号位")
#3.2.3从列表中删除元素
#使用del语句删除元素
del motorcycles[0]
print(motorcycles,"使用python函数del删除0号位元素")
#使用pop删除元素 从末尾删除 并能够继续使用 列表相当于一个栈 pop弹出栈顶元素
popped_motorcycles=motorcycles.pop()
print(popped_motorcycles,"从栈顶弹出的元素")
#使用pop加索引位置,删除列表指定位置元素
first_owned=motorcycles.pop(0)
print(first_owned,"使用pop+索引位置,弹出指定位置元素")
#使用remove根据元素值删除元素 值注意的是 remove只删除满足要求的第一个元素
print(motorcycles)
motorcycles.remove(motorcycles[0])
print(motorcycles,"使用remove按值删除元素")
#Test
persions=['wyl','zsy','wq','hzx']
words=" :I want eat dinner with you,"
for name in persions:
name=name+words
print(name)
print(persions[0],"is can not go to this dinner")
persions[0]="ooo"
for name in persions:
name=name+words
print(name)
print("I had found a bigger table")
persions.insert(0,"new_persion_in_first")
persions.append("new_persion_in_lower")
persions.insert(round(len(persions)/2),"insert_persion")
print(persions)
for name in persions:
name=name+words
print(name)
print("new I can noly invite two persion!")
while len(persions)>2:
name=persions.pop()
words="Im very sorry for you "
print(name+words)
print(persions)
for name in persions:
words="you are always in invite_list"
print(name+words)
del persions[0]
del persions[0]
print(persions)
组织列表
#3.3.1使用方法sort对列表永久排序
cars=['bmw','audi','toyota','subaru']
cars.sort() #默认按照字母序排列 永久性排序
print(cars)
cars.sort(reverse=True) #按照字母序的反序列排序
print(cars)
print(sorted(cars,reverse=True),"sorted临时排序,不影响原列表顺序")
#3.3.3倒着打印列表
#反转列表可以使用reverse函数。
print(cars)
cars.reverse()
print(cars)
#3.3.4确定列表长度 长度从1开始计算
print(len(cars))
#Test
city=["挪威","芬兰","美国","英国","法国","荷兰"]
print(sorted(city)) #临时排序
print(sorted(city,reverse=True)) #临时排序 按字母序反方向
print(city.reverse()) #反转列表
print(city.reverse())
print(city.sort())
print(city.sort(reverse=True))
print("I want intive ",len(city))
索引错误
#使用列表时避免索引错误
motorcycles=['honda','yamaha','suzuki']
print(motorcycles[2]) #列表长度位3 所以最大索引是3-1=2
print(motorcycles[-1]) #当列表为空时,-1位置才会报错