Chap6 列表

Chap6 列表

 

 

 

Chap6 列表

 

 

 

# 开发人员:雪糕超人

# 开发时间:2021/7/17 10:22
a=10 #变量存储的是一个对象的引用
lst=[‘hello‘,‘world‘,98]
print(id(lst))
print(type(lst))
print(lst)

Chap6 列表

 

 

 Chap6 列表

 

 

 

# 开发人员:雪糕超人

# 开发时间:2021/7/17 10:28
‘‘‘创建列表的第一种方式,使用[]‘‘‘
lst=[‘hello‘,‘world‘,98]
‘‘‘创建列表的第二种方式,使用内置函数list()‘‘‘
lst2=list([‘hello‘,‘world‘,98])
print(id(lst),id(lst2))
print(type(lst),type(lst2))
print(lst,lst2)

Chap6 列表

 

 

 

 Chap6 列表

 

 

 

# 开发人员:雪糕超人

# 开发时间:2021/7/17 10:28
‘‘‘创建列表的第一种方式,使用[]‘‘‘
lst=[‘hello‘,‘world‘,98,‘hello‘]
print(lst[0],lst[-4])
‘‘‘创建列表的第二种方式,使用内置函数list()‘‘‘
lst2=list([‘hello‘,‘world‘,98])
print(id(lst),id(lst2))
print(type(lst),type(lst2))
print(lst,lst2)

Chap6 列表

 

 

 

 

# 开发人员:雪糕超人

# 开发时间:2021/7/17 10:39
lst=[‘hello‘,‘world‘,98,‘hello‘]
print(lst.index(‘hello‘)) # 如果列表中有相同元素则返回的只是相同元素中的第一个元素的索引
#print(lst.index(‘Python‘)) # ValueError: ‘Python‘ is not in list
#print(lst.index(‘hello‘,1,3)) # ValueError: ‘hello‘ is not in list ‘world‘,98
print(lst.index(‘hello‘,1,4))

Chap6 列表

 

 

Chap6 列表

 

 

 Chap6 列表

 

 

 

# 开发人员:雪糕超人

# 开发时间:2021/7/17 10:48
lst=[‘hello‘,‘world‘,98,‘hello‘,‘hello‘,‘world‘,234]
#获取索引为2的元素
print(lst[2])
#获取索引为-3的元素
print(lst[-3])

Chap6 列表

 

 

 Chap6 列表

 

 

 

# 开发人员:雪糕超人

# 开发时间:2021/7/17 10:52
lst=[10,20,30,40,50,60,70,80]
#start=1,stop=6,step=1
#print(lst[1:6:1])
print(‘原列表id‘,id(lst))
lst2=lst[1:6:1]
print(‘切完列表id‘,id(lst2))
print(lst[1:6])#默认步长为1
print(lst[1:6:])#默认步长为1
#start=1,stop=6,step=2
print(lst[1:6:2])
#stop=6,step=2 start采取默认
print(lst[:6:2])
#start=1,step=2 stop采取默认
print(lst[1::2])
print(‘------------------step步长为负数的情况---------------------‘)
print(‘原列表‘,lst)
print(‘切列表‘,lst[::-1])
#start=7,step=-1 stop采取默认
print(lst[7::-1])
#start=6,stop=0,step=-2
print(lst[6:0:-2])

Chap6 列表

 

 

 Chap6 列表

 

 

 

# 开发人员:雪糕超人

# 开发时间:2021/7/17 14:19
print(‘p‘ in ‘python‘)#True
print(‘k‘ not in ‘python‘)#True

lst=[10,20,‘python‘,‘hello‘]
print(‘10‘ in lst)#False
print(‘100‘ in lst)#False
print(‘10‘ not in lst)#True
print(‘10‘ not in lst) #True

print(‘------------------------------------‘)
for item in lst:
print(item)

Chap6 列表

 

 

 Chap6 列表

 

 

 

# 开发人员:雪糕超人

# 开发时间:2021/7/17 14:24
#向列表的末尾添加一个元素
lst=[10,20,30]
print(‘添加元素之前‘,lst,id(lst))
lst.append(100)
print(‘添加元素之后‘,lst,id(lst))

Chap6 列表

 

 

 

# 开发人员:雪糕超人

# 开发时间:2021/7/17 14:24
#向列表的末尾添加一个元素
lst=[10,20,30]
print(‘添加元素之前‘,lst,id(lst))
lst.append(100)
print(‘添加元素之后‘,lst,id(lst))

lst2=[‘hello‘,‘world‘]
#lst.append(lst2)#将lst2作为一个元素添加到lst末尾
lst.extend(lst2)#向列表末尾一次性添加多个元素
print(lst)

#在任意位置上添加一个元素
lst.insert(1,90)
print(lst)

#在任意位置添加N多个元素
lst3=[True,False,‘hello‘]
lst[1:]=lst3
print(lst)

Chap6 列表

 

 

# 开发人员:雪糕超人

# 开发时间:2021/7/17 14:37
lst=[10,20,30,40,50,60,30]
#lst.remove()一次删除一个元素
lst.remove(30)#从列表中移除一个元素,如果有重复元素只移除第一个元素
print(lst)
#lst.remove(100) #ValueError: list.remove(x): x not in list

#pop()根据索引移除元素
lst.pop(1)
print(lst)
#lst.pop(5)#IndexError: pop index out of range 如果指定的索引位置不存在,将抛出异常
lst.pop()#如果不指定(参数)索引,则删除列表中最后一个元素
print(lst)

#切片操作-删除至少一个元素,将产生一个新的列表对象
new_lst=lst[1:3]
print(‘原列表‘,lst)
print(‘切片后的列表‘,new_lst)

#不产生新的对象,而是删除原列表中的内容
lst[1:3]=[]
print(lst)

#清除列表中的所有元素
lst.clear()
print(lst)

#将列表语句删除
del lst
#print(lst) #NameError: name ‘lst‘ is not defined

Chap6 列表

 

 Chap6 列表

 

 Chap6 列表

 

 

# 开发人员:雪糕超人

# 开发时间:2021/7/17 15:07
lst=[10,20,30,40]
#一次修改一个值
lst[2]=100
print(lst)
lst[1:3]=[300.400,500,600]
print(lst)

Chap6 列表

 

 Chap6 列表

 

 

# 开发人员:雪糕超人

# 开发时间:2021/7/17 15:10
lst=[20,40,60,98,54]
print(‘排序前的列表:‘,lst,id(lst))
lst.sort()
print(‘排序后的列表:‘,lst,id(lst))

#通过指定关键数参数,将列表中的元素惊醒降序排序
lst.sort(reverse=True)#reverse=True表示降序排序
print(‘排序后的列表:‘,lst,id(lst))
lst.sort(reverse=False)#reverse=False表示升序排序
print(‘排序后的列表:‘,lst,id(lst))

#使用内置函数sorted()对列表进行排序,将产生一个新的列表对象
lst=[20,10,60,98,54]
print(‘原列表对象lst:‘,lst,id(lst))
new_lst=sorted(lst)
print(‘新列表对象new:‘,new_lst,id(new_lst))
#指定关键字参数,实现列表的降序排序
desc_list=sorted(lst,reverse=True)
print(‘新列表对象desc_list:‘,desc_list,id(desc_list))

Chap6 列表

 

Chap6 列表

上一篇:针孔相机成像模型


下一篇:一阶常系数线性差分方程通解求法