zoukankan      html  css  js  c++  java
  • 1.2

      1 1.创建列表
      2 >>> names = ['old_driver', 'rain', 'jack', 'shanshan', 'peiqi', 'black_girl']
      3 >>> names
      4 ['old_driver', 'rain', 'jack', 'shanshan', 'peiqi', 'black_girl'] 
      5 
      6 2.插入alex
      7 >>> names.insert(-1,'alex')
      8 >>> names
      9 ['old_driver', 'rain', 'jack', 'shanshan', 'peiqi', 'alex', 'black_girl']
     10 
     11 3.shanshan改成中文
     12 >>> names
     13 ['old_driver', 'rain', 'jack', 'shanshan', 'peiqi', 'alex', 'black_girl']
     14 >>> names.index('shanshan')
     15 3
     16 >>> names[names.index('shanshan')] = "珊珊"
     17 >>> names
     18 ['old_driver', 'rain', 'jack', '珊珊', 'peiqi', 'alex', 'black_girl']
     19 
     20 4.rain后面插一个子列表 
     21 >>> names.insert(2,['oldboy','oldgirl'])
     22 >>> names
     23 ['old_driver', 'rain', ['oldboy', 'oldgirl'], 'jack', '珊珊', 'peiqi', 'alex', 'black_girl']
     24 
     25 5.得到peiqi得索引值
     26 >>> names.index('peiqi')
     27 5
     28 
     29 6.创建新列表并合入names列表中
     30 >>> s = [1,2,3,4,2,5,6,2]
     31 >>> s
     32 [1, 2, 3, 4, 2, 5, 6, 2]
     33 >>> names.extend(s)
     34 >>> names
     35 ['old_driver', 'rain', ['oldboy', 'oldgirl'], 'jack', '珊珊', 'peiqi', 'alex', 'black_girl', 1, 2, 3, 4, 2, 5, 6, 2]
     36 
     37 7.取出names列表4-7得元素
     38 >>> names
     39 ['old_driver', 'rain', ['oldboy', 'oldgirl'], 'jack', '珊珊', 'peiqi', 'alex', 'black_girl', 1, 2, 3, 4, 2, 5, 6, 2]
     40 >>> names[4:7]
     41 ['珊珊', 'peiqi', 'alex']
     42 
     43 8.取出name列表2-10得元素步长为2
     44 >>> names[2:10:2]
     45 [['oldboy', 'oldgirl'], '珊珊', 'alex', 1]
     46 
     47 9.取出列表最后3个元素
     48 >>> names[-3:]
     49 [5, 6, 2]
     50 
     51 10.循环列表 打印每个元素得索引值和元素
     52 >>> for i in names:
     53     print(names.index(i),  i)
     54 
     55     
     56 0 old_driver
     57 1 rain
     58 2 ['oldboy', 'oldgirl']
     59 3 jack
     60 4 珊珊
     61 5 peiqi
     62 6 alex
     63 7 black_girl
     64 8 1
     65 9 2
     66 10 3
     67 11 4
     68 9 2
     69 13 5
     70 14 6
     71 9 2
     72 >>> 
     73 
     74 >>> count = 0
     75 >>> for i in names:
     76     print(count,i)
     77     count += 1
     78 
     79     
     80 0 old_driver
     81 1 rain
     82 2 ['oldboy', 'oldgirl']
     83 3 jack
     84 4 珊珊
     85 5 peiqi
     86 6 alex
     87 7 black_girl
     88 8 1
     89 9 2
     90 10 3
     91 11 4
     92 12 2
     93 13 5
     94 14 6
     95 15 2
     96 >>> 
     97 
     98 >>> for i in enumerate(names):
     99     print(i)
    100 
    101     
    102 (0, 'old_driver')
    103 (1, 'rain')
    104 (2, ['oldboy', 'oldgirl'])
    105 (3, 'jack')
    106 (4, '珊珊')
    107 (5, 'peiqi')
    108 (6, 'alex')
    109 (7, 'black_girl')
    110 (8, 1)
    111 (9, 2)
    112 (10, 3)
    113 (11, 4)
    114 (12, 2)
    115 (13, 5)
    116 (14, 6)
    117 (15, 2)
    118 >>> 
    119 
    120 >>> for index,i in enumerate(names):
    121     print(index,i)
    122 
    123     
    124 0 old_driver
    125 1 rain
    126 2 ['oldboy', 'oldgirl']
    127 3 jack
    128 4 珊珊
    129 5 peiqi
    130 6 alex
    131 7 black_girl
    132 8 1
    133 9 2
    134 10 3
    135 11 4
    136 12 2
    137 13 5
    138 14 6
    139 15 2
    140 >>> 
    141 
    142 11.循环列表 打印索引值和元素 索引值为偶数时 对应得元素改成1
    143 for index,i in enumerate(names):
    144     if index % 2 == 0:
    145         names[index] = 0
    146 print(names)
    147 
    148 12.names有3个2 返回第二个2得索引值 
    149 names = [-1, 'rain', -1, 'jack', -1, 'peiqi', -1, 'black_girl', -1, 2, -1, 4, -1, 5, -1, 2, 2]
    150 first_index = names.index(2)
    151 new_list = names[first_index+1:]
    152 second_index = new_list.index(2)
    153 second_val = names[first_index+second_index+1]
    154 print(new_list,first_index,second_index)
    155 print("second_val:",second_val)
    156 
    157 13.打印商品列表
    158 products = [['Iphone8',6888],['MacPro',14800],['小米6',2499],['Coffee',31],['Book',80],['Nike Shoes',799]]
    159 print("-------商品列表------")
    160 for index, i in enumerate(products):
    161     print("%s. %s    %s" % (index, i[0], i[1]))
    162 
    163 
    164 14.
    165 # 14 商品 不断问用户想买什么 用户选择一个商品编号 放在购物车里 最终用户输入q退出 打印 购物车商品列表 
    166 products = [['Iphone8',6888],['MacPro',14800],['小米6',2499],['Coffee',31],['Book',80],['Nike Shoes',799]]
    167 shopping_cart = []
    168 
    169 # run_flag = True
    170 exit_flag = False
    171 
    172 while not exit_flag:
    173     print("-------商品列表------")
    174     for index, i in enumerate(products):
    175         print("%s. %s    %s" % (index, i[0], i[1]))
    176 
    177     choice = input("输入想买得商品编号:")
    178     if choice.isdigit():
    179         choice = int(choice)
    180         if choice >= 0  and choice < len(products):
    181             shopping_cart.append(products[choice])
    182             print("Added products %s into shoppong_cart" % (products[choice]))
    183         else:
    184             print("商品不存在")
    185     elif choice == 'q':
    186         if len(shopping_cart) > 0:
    187             print("------------你已购买以下商品----------")
    188             for index,i in enumerate(shopping_cart):
    189                 print("%s. %s    %s" % (index, i[0], i[1]))
    190        # break
    191        # run_flag = False
    192         exit_flag = True
  • 相关阅读:
    DAG:区块链行业下一个引爆点?
    php7的新特性
    Linux中的冷热页机制概述
    撰写后台需求文档需要注意的那些事儿
    poj 1201 Intervals
    poj 1364
    poj Candies
    hdu 1429
    poj A Round Peg in a Ground Hole
    poj 1113Wall
  • 原文地址:https://www.cnblogs.com/alice-bj/p/8427401.html
Copyright © 2011-2022 走看看