您的位置:首页 > 编程语言 > Python开发

python数据类型-列表练习

2018-03-02 13:15 387 查看


自己做的:# # name = 'albert'
# # for i in name :
# # print(i)
# #一个列表name通过for 临时变量j in name来循环
# name = ['albert','boy','23',1995,6,1]
# for j in name:
# # print(name.index(j),j)
# if name.index(j)%2 == 0:
# name[name.index(j)] = '-1'
# print(name.index(j))
# 1.
names = ['old_driver','rain','jack','shanshan','peiqi','black_girl']
# 2.
names.insert(names.index('black_girl'),'alex')
#3.
names[names.index('shanshan')]='姗姗'
# 4.
names.insert(names.index('rain')+1,['oldboy','oldgirl'])
# 5.
n = names.index('peiqi')
# 6.
names2 = [1,2,3,4,2,5,6,2]
names = names + names2
# 7.
n2=names[4:8]
# 8.
n3=names[2:11:2]
# 9.
n4 = names[-3:]
# 10.
# for i in names:
# print('loop:',names.index(i),i)
# 11.
# for i in names:
# print('loop:',names.index(i),i)
# if names.index(i) %2 == 0:
# names[names.index(i)] = '-1'
# print(names)
#12.
names[names.index(2)] = '第一个2'
print(names.index(2))
# 13.
products = [['Iphone8',6888],['Mac Pro',14800],['小米6',2499],['Coffee',31],['Book',80],['Nike Shoes',799]]

print('------------------商品列表------------------')
for k in products:
print((products.index(k)),'.',k)

# 14.
products = [['Iphone8',6888],['Mac Pro',14800],['小米6',2499],['Coffee',31],['Book',80],['Nike Shoes',799]]
shopping = []
# print('-------------商品列表-------------')
# for index,p in enumerate(products):
#     print(''' %s  %s  %s ''' %(index,p[0],p[1]))
while True:
info = ''' %s    %s    %s '''
print('-------------商品列表-------------')

for index,p in enumerate(products):
print(info %(index,p[0],p[1]))
choose = int(input("请选择要买的商品编号:"))
print("您的商品如下:",'''%s   %s  ''' %((products[choose])[0],(products[choose])[1])   )
shopping.insert(choose,products[choose])
print(shopping)
第11题:names = ['old_driver','rain','jack','shanshan','peiqi','black_girl']
count = 0
for i in names:

count += 1
if count % 2== 0:
i = '-1'
print(count, i)
还可以用枚举:enumerate
for i in enumerate(names):
print(i)



去括号:for index,i in enumerate(names):
print(index,i)



第12题:names = ['old_driver','rain','jack','shanshan','peiqi','black_girl',1,2,3,4,2,5,6,2]
count = 0
for i in names:
if i == 2:
print(count)
count += 1       正确答案:names = ['old_driver','rain','jack','shanshan','peiqi','black_girl',1,2,3,4,2,5,6,2]
first_val = names.index(2)
new_names=names[first_val+1:]
second_val = new_names.index(2)
print('second 2 is:',first_val+second_val+1)第13题:
products = [['Iphone8',6888],['Mac Pro',14800],['小米6',2499],['Coffee',31],['Book',80],['Nike Shoes',799]]
# print('-------------商品列表-------------')
# for index,p in enumerate(products):
#     print(''' %s  %s  %s ''' %(index,p[0],p[1]))
info = ''' %s    %s    %s '''
print('-------------商品列表-------------')

for index,p in enumerate(products):
print(info %(index,p[0],p[1]))
第14题:
1.products = [['Iphone8',6888],['Mac Pro',14800],['小米6',2499],['Coffee',31],['Book',80],['Nike Shoes',799]]
shopping_cart = []
# print('-------------商品列表-------------')
# for index,p in enumerate(products):
# print(''' %s %s %s ''' %(index,p[0],p[1]))
while True:
info = ''' %s %s %s '''
print('-------------商品列表-------------')

for index,p in enumerate(products):
print(info %(index,p[0],p[1]))

choose = input("请选择要买的商品编号:")
if choose.isdigit() :
choose = int(choose)
print("您的商品如下:",'''%s %s ''' %((products[choose])[0],(products[choose])[1]) )
shopping_cart.append(products[choose])

elif choose == 'q':
print('----------已购买商品如下----------')
for index,p in enumerate(shopping_cart):
print(''' %s %s %s''' %(index,p[0],p[1]))
break
2.
products =
ac71
[['Iphone8',6888],['Mac Pro',14800],['小米6',2499],['Coffee',31],['Book',80],['Nike Shoes',799]]
shopping_cart = []     命名要规范
# print('-------------商品列表-------------')
# for index,p in enumerate(products):
#     print(''' %s  %s  %s ''' %(index,p[0],p[1]))
run_flag = True        ######注意
while run_flag:
info = ''' %s    %s    %s '''
print('-------------商品列表-------------')

for index,p in enumerate(products):     #枚举
print(info %(index,p[0],p[1]))

choose = input("请选择要买的商品编号:")
if choose.isdigit() :     ##判断是否为数
choose = int(choose)
if choose >= 0 and choose < len(products) :   #len(products)表示products列表长度    ##判断商品是否存在
print("您的商品如下:",'''%s   %s  ''' %((products[choose])[0],(products[choose])[1])   )
shopping_cart.append(products[choose])
else:
print("您的商品不存在")
elif choose == 'q':
if len(shopping_cart) > 0:
print('----------已购买商品如下----------')
for index,p in enumerate(shopping_cart):
print(''' %s   %s   %s''' %(index,p[0],p[1]))
# break
run_flag = False

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: