1. 程式人生 > >2 -4 -1 list列表練習解答

2 -4 -1 list列表練習解答

black new cart choose pen attr ever 枚舉 --

技術分享圖片

答案

1

>>> names = []
>>> names.append(old_driver)
>>> names.append(rain)
>>> names.append(jack)
>>> names.append(shanshan)
>>> names.append(peiqi)
>>> names.append(black_girl)
>>> names
[old_driver, rain
, jack, shanshan, peiqi, black_girl]
#list方法
>>> dir(names)
[__add__, __class__, __contains__, __delattr__, __delitem__, __dir__, __doc__, __eq__, __format__, __ge__, __getattribute__, __getitem__, __gt__, __hash__, __iadd__, __imul__, __init__
, __iter__, __le__, __len__, __lt__, __mul__, __ne__, __new__, __reduce__, __reduce_ex__, __repr__,__reversed__, __rmul__, __setattr__, __setitem__, __sizeof__, __str__, __subclasshook__, append, clear, copy, count, extend, index, insert, pop,
remove, reverse, sort]

2

>>> names.index(black_girl)
5
>>> names.insert(5,alex)
>>> names
[old_driver, rain, jack, shanshan, peiqi, alex, black_girl]

3

>>> names[names.index(shanshan)] = 姍姍
>>> names
[old_driver, rain, jack, 姍姍, peiqi, alex, black_girl]

4

>>> names2
[oldboy, oldgirl]
>>> names.index(rain)
>>> names.insert(2,names2)
>>> names
[old_driver, rain, [oldboy, oldgirl], jack, 姍姍, peiqi, alex, black_girl]

5

>>> names.index(peiqi)
5

6

>>> names3 = [1,2,3,4,2,5,6,2]
>>> names.extend(names3)
>>> names
[old_driver, rain, [oldboy, oldgirl], jack, 姍姍, peiqi, alex, black_girl, 1, 2, 3, 4, 2, 5, 6, 2]

>>> names4 = names + names3
>>> names4
[old_driver, rain, [oldboy, oldgirl], jack, 姍姍, peiqi, alex, black_girl, 1, 2, 3, 4, 2, 5, 6, 2]

>>>  names.append(names3)
>>> names
[old_driver, rain, [oldboy, oldgirl], jack, 姍姍, peiqi, alex, black_girl, [1, 2, 3, 4, 2, 5, 6, 2]]

7-9

>>> names[4:8]
[姍姍, peiqi, alex, black_girl]
>>>

>>> names[2:11:2]
[[oldboy, oldgirl], 姍姍, alex, [1, 2, 3, 4, 2, 5, 6, 2]]
>>>
>>> names[-3:]
[alex, black_girl, [1, 2, 3, 4, 2, 5, 6, 2]]

10

>>> for k in names:
...     print(names.index(k),k)

names3 = [old_driver, rain, jack, shanshan, peiqi, black_girl]

count = 0
for i in names3:
    print(count,i)
    count += 1
#運行結果
old_driver
rain
[oldboy, oldgirl]
jack
姍姍
peiqi
alex
black_girl
[1, 2, 3, 4, 2, 5, 6, 2]

for i in enumerate(names3):  #枚舉
    print(i)

# 運行結果
(0, old_driver)
(1, rain)
(2, jack)
(3, shanshan)
(4, peiqi)
(5, black_girl)
for index,item in enumerate(names3):  #枚舉
    print(index,item)

# 運行結果
old_driver
rain
jack
shanshan
peiqi
black_girl

11

names = [old_driver, rain, [oldboy, oldgirl], jack, 姍姍, peiqi, alex, black_girl, [1, 2, 3, 4, 2, 5, 6, 2]]

for k in names:
    index = names.index(k)
  if index%2 == 0:
      names[index] = -1
      print(index, k)
print(names)

names3 = [old_driver, rain, jack, shanshan, peiqi, black_girl]

for index,item in enumerate(names3):
    if index%2 == 0:
        print(index,item)
        names3[index] = -1
print(names3)

12

技術分享圖片

技術分享圖片

13

product = [[iphone,6888],[macpro,14800],[小米6,2499],[coffee,31],[book,80],[nike shoes,799]]
print("-----商品列表-----")
for index,item in enumerate(product):
    msg = "%s %s %s"%(index,product[index][0],product[index][-1])
    print(msg)

#運行結果
-----商品列表-----
0 iphone 6888
1 macpro 14800
2 小米6 2499
3 coffee 31
4 book 80
5 nike shoes 799

Process finished with exit code 0

另一種

product = [[iphone,6888],[macpro,14800],[小米6,2499],[coffee,31],[book,80],[nike shoes,799]]
print("-----商品列表-----")
for index,item in enumerate(product):
    msg = "%s %s %s"%(index,item[0],item[-1])
    print(msg)

#運行結果
-----商品列表-----
0 iphone 6888
1 macpro 14800
2 小米6 2499
3 coffee 31
4 book 80
5 nike shoes 799

Process finished with exit code 0

購物車程序

技術分享圖片

product = [[iphone,6888],[macpro,14800],[小米6,2499],[coffee,31],[book,80],[nike shoes,799]]
shopping_cart = []
while True:
    print("------商品列表------")
    for index,i in enumerate(product):
        print("%s  %s  %s"%(index,i[0],i[1]))
    choose = input("輸入商品編號(0-5),退出(q)")
    if choose.isdigit():
        choose =int(choose)
        if choose < len(product):
            shopping_cart.append(product[choose])
            print("商品已加入購物車!%s" %(product[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
    else:
        print("輸入有誤,請重新輸入")

#運行結果
輸入商品編號(0-5),退出(q)2
商品已加入購物車![小米6, 2499]
------商品列表------
0  iphone  6888
1  macpro  14800
2  小米6  2499
3  coffee  31
4  book  80
5  nike shoes  799
輸入商品編號(0-5),退出(q)q
-----你的購物車------
0   macpro   14800   
1   小米6   2499   
Process finished with exit code 0

技術分享圖片

2 -4 -1 list列表練習解答