列表学习细节
names=['a','b','c']
numbers=[1,2,3]
kongs=[]
kong2=list()
print(names,numbers,kongs,kong2)
一般都用names=['a','b','c'] 和 names=[] 两种形式
names=['a','b',1,-1]
names[0]
'a'
names[-1]
-1
通过索引或下标取值,从0开始,倒数第一个就是-1
列表是可以重复的!
l01=['1','123','sdfa',0,1,2,33333,444,2,32,2,2,2,2,2,5,-6,-9,'gg']
print(l01)
查找
可见,找到的是第一个符合的下标
没有的元素会报错:
l01[l01.index(444)]
444
计数:
l01.count(2)
7
l01里有7个2
——————————————————————
切片
取其中一部分
l01[0:3]
['1', '123', 'sdfa']
顾头不顾尾,光标所在的段是截取的值
我想取444到-6的段
l01=['1','123','sdfa',0,1,2,33333,444,2,32,2,2,2,2,2,5,-6,-9,'gg']
print(l01)
idx1=l01.index(444)
idx2=l01.index(-6)
print(idx1,idx2)
g1=l01[idx1:idx2+1]
print(g1)
['1', '123', 'sdfa', 0, 1, 2, 33333, 444, 2, 32, 2, 2, 2, 2, 2, 5, -6, -9, 'gg']
7 16
[444, 2, 32, 2, 2, 2, 2, 2, 5, -6]
——————————————————————-
取最后5个
g2=l01[-5:]
print(g2)
[2, 5, -6, -9, 'gg']
去前3个
g3=l01[:3]
print(g3)
取全部
print(l01[:])
从第2个到倒数第3个,每3个一取,3是步长
print(l01[1:-2:3])
print(l01[::2])
————————————————
n1=[1,2,3,'a','b','c','d']
n1.append('e')
print(n1)
[1, 2, 3, 'a', 'b', 'c', 'd', 'e']
n1.insert(0,0)
n1
[0, 1, 2, 3, 'a', 'b', 'c', 'd', 'e']
n1.insert(3,2.5)
n1
[0, 1, 2, 2.5, 3, 'a', 'b', 'c', 'd', 'e']
在c前插入B2C
n1.insert(n1.index('c'),'b2c')
n1
[0, 1, 2, 2.5, 3, 'a', 'b', 'b2c', 'c', 'd', 'e']
在a后插入‘a-b’
n1.insert(n1.index('a')+1,'a-b')
n1
[0, 1, 2, 2.5, 3, 'a', 'a-b', 'b', 'b2c', 'c', 'd', 'e']
修改就是直接赋值
n1[n1.index('c')]='c2c'
n1
[0, 1, 2, 2.5, 3, 'a', 'a-b', 'b', 'b2c', 'c2c', 'd', 'e']
多值修改,少用
n1
[0, 1, 2, 2.5, 3, 'a', 'a-b', 'b', 'b2c', 'c2c', 'd', 'e']
n1[2:4]='xyz uvw'
n1
[0, 1, 'x', 'y', 'z', ' ', 'u', 'v', 'w', 3, 'a', 'a-b', 'b', 'b2c', 'c2c', 'd', 'e']
注意,不是替换成一个,而是拆分成单个字母,挤入,空格也算。
————————————————————————
n1
[0, 1, 'x', 'y', 'z', ' ', 'u', 'v', 'w', 3, 'a', 'a-b', 'b', 'b2c', 'c2c', 'd', 'e']
n1.pop()
'e'
n1
[0, 1, 'x', 'y', 'z', ' ', 'u', 'v', 'w', 3, 'a', 'a-b', 'b', 'b2c', 'c2c', 'd']
pop弹出,删除最后一个
删除第一个x元素
n1.remove('x')
n1
[0, 1, 'y', 'z', ' ', 'u', 'v', 'w', 3, 'a', 'a-b', 'b', 'b2c', 'c2c', 'd']
del全局删除,不只是删列表,删的是下标
del n1[2]
n1
[0, 1, 'z', ' ', 'u', 'v', 'w', 3, 'a', 'a-b', 'b', 'b2c', 'c2c', 'd']
del想删什么就删什么
del names
names
Traceback (most recent call last):
File "<input>", line 1, in <module>
NameError: name 'names' is not defined
批量删除
n1
[0, 1, 'z', ' ', 'u', 'v', 'w', 3, 'a', 'a-b', 'b', 'b2c', 'c2c', 'd']
del n1[2:5]
n1
[0, 1, 'v', 'w', 3, 'a', 'a-b', 'b', 'b2c', 'c2c', 'd']
循环打印列表元素
n1=[1,2,3,'a','b','c','d']
for i in n1:
print(i)
n3=range(10)
n3
range(0, 10)
for i in n3:
print(i)
0
1
2
3
4
5
6
7
8
9
range(10)就是生成0-9的列表
for i in range(10):
print('row ',i)
for和while的区别:
for是有边界的,while可以是死循环
n1=[9,-2,42,7,'99','jack','E',0]
n1
[9, -2, 42, 7, '99', 'jack', 'E', 0]
列表是有序的,因为它是有索引下标的。
n1.sort()
Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: '<' not supported between instances of 'str' and 'int'
print(n1)
n1.sort()
print(n1)
['99', 'TT', 'a', 'jack', 'E', 'T']
['99', 'E', 'T', 'TT', 'a', 'jack']
是按照编码顺序排序的
n1.reverse()
print(n1)
['99', 'E', 'T', 'TT', 'a', 'jack']
['jack', 'a', 'TT', 'T', 'E', '99']
反转
————————————————
列表拼接
n1=[1,2,3]
n2=['a','b','c']
n1+n2
[1, 2, 3, 'a', 'b', 'c']
n2.extend(n1)
n2
['a', 'b', 'c', 1, 2, 3]
n1
[1, 2, 3]
清空
n1.clear()
print(n1)
n1=[1,2,3]
n2=n1
n2
[1, 2, 3]
n1[1]='two'
n1
[1, 'two', 3]
n2
[1, 'two', 3]
列表赋值,是会指向同一个列表的。
n1
[1, 'two', 3]
n3=n1.copy()
n3
[1, 'two', 3]
n1[1]
'two'
n1[1]='second'
n1
[1, 'second', 3]
n3
[1, 'two', 3]
用了copy方法,则n1和n3是独立的,不影响。
——————————————————————————————
# coding:utf-8
# 1.创建一个空列表,命名为names,往里面添加old_driver,rain,jack,shanshan,peiqi,black_girl 元素
names=[]
names.append('old_driver')
names.append('rain')
names.append('jack')
names.append('shanshan')
names.append('peiqi')
names.append('black_girl')
print(names)
# 2.往names列表里black_girl前面插入一个alex
names.insert(names.index('black_girl'),'alex')
print(names)
# 3.把shanshan的名字改成中文,姗姗
names[names.index('shanshan')]='姗姗'
print(names)
# 4.往names列表里rain的后面插入一个子列表,[oldboy, oldgirl]
names.insert(names.index('rain')+1,['oldboy','oldgirl'])
print(names)
# 5.返回peiqi的索引值
print(names.index('peiqi'))
# 6.创建新列表[1,2,3,4,2,5,6,2],合并入names列表
n1=[1,2,3,4,2,5,6,2]
names.extend(n1)
print(names)
# 7.取出names列表中索引4-7的元素
g1=names[4:8]
print(g1)
# 8.取出names列表中索引2-10的元素,步长为2
g2=names[2:11:2]
print(g2)
# 9.取出names列表中最后3个元素
g3=names[-3:]
print(g3)
# 10.循环names列表,打印每个元素的索引值,和元素
count=0
for i in names:
print(count,'-',i)
count+=1
for i in enumerate(names):
print(type(i))
print(i)
for index,i in enumerate(names):
print(index,', ',i)
# 11.循环names列表,打印每个元素的索引值,和元素,当索引值 为偶数时,把对应的元素改成-1
count=0
for i in names:
if count%2==0:
print(count, ' ', -1)
else:
print(count,' ',i)
count+=1
for index,i in enumerate(names):
if index%2==0:
print(index,', ',-1)
else:
print(index,', ',i)
for index,i in enumerate(names):
if index%2==0:
names[index]=-1
print(names)
# 12.names里有3个2,请返回第2个2的索引值。不要人肉数,要动态找(提示,找到第一个2的位置,在此基础上再找第2个)
names2=names.copy()
names2[names2.index(2)]='displace'
print(names)
print(names2)
print(names2.index(2))
print(names)
idx1=names.index(2)
idx2=names[idx1+1:].index(2)
idx3=idx1+idx2+1
print(idx3,names[idx3])
# 13.现有商品列表如下:
# products = [ ['Iphone8',6888],['MacPro',14800], ['小米6',2499],['Coffee',31],['Book',80],['Nike Shoes',799] ]
# 需打印出这样的格式:
#
# ---------商品列表----------
# 0. Iphone8 6888
# 1. MacPro 14800
# 2. 小米6 2499
# 3. Coffee 31
# 4. Book 80
# 5. Nike Shoes 799
products=[['Iphone8',6888],['MacPro',14800],['小米6',2499],['Coffee',31],['Book',80],['Nike shoes',799]]
print('----------商品列表---------')
for i in products:
print(products.index(i),'. ',i[0],' ',i[1])
for index,p in enumerate(products):
print('%s. %s %s' % (index,p[0],p[1]))
14. 写一个循环,不断的问用户想买什么,用户选择一个商品编号,就把对应的商品添加到购物车里, 最终用户输入q退出时,打印购物车里的商品列表
products = [ ['Iphone8',6888],['MacPro',14800], ['小米6',2499],['Coffee',31],['Book',80],['Nike Shoes',799] ]
cart=[]
exit_flag=False
while not exit_flag:
print('-----------products list---------------')
for index,p in enumerate(products):
print('%s. %s %s' % (index,p[0],p[1]))
label=input('please input the number which you want to buy (q=exit) :')
if label.isdigit()==True:
if int(label) >=0 and int(label)<len(products):
cart.append(products[int(label)])
print('this has been added to cart: ',products[int(label)])
else:
print('there is not this products.')
elif label=='q':
if len(cart)>0:
print('your cart:')
for index,p in enumerate(cart):
print('%s. %s %s' % (index,p[0],p[1]))
else:
pass
exit_flag=True