列表list
- 结果类型 中括号括起来
- 逗号(,)分割每一个元素
- 列表中的元素可以是数字,字符串,列表,布尔值所有的都能放
- 索引,切片
增
#append 增加新数据到列表最后 li = [‘alex‘,‘wusir‘,‘egon‘,‘女神‘,‘taibai‘] li.append(‘lidongxu‘) print(li) #输出:[‘alex‘, ‘wusir‘, ‘egon‘, ‘女神‘, ‘taibai‘, ‘lidongxu‘] #insert 增加新数据到列表指定位置(插入) li = [‘alex‘, ‘wusir‘, ‘egon‘, ‘女神‘, ‘taibai‘, ‘lidongxu‘] li.insert(2,‘liuyi‘) print(li) #输出:[‘alex‘, ‘wusir‘, ‘liuyi‘, ‘egon‘, ‘女神‘, ‘taibai‘, ‘lidongxu‘] #extend 迭代添加 数据会被分解到最小元素 例:‘小明‘ 会被分解为 ‘小‘, ‘明‘ li = [‘alex‘, ‘wusir‘, ‘liuyi‘, ‘egon‘, ‘女神‘, ‘taibai‘, ‘lidongxu‘] li.extend(‘小明‘) print(li) #输出结果:[‘alex‘, ‘wusir‘, ‘liuyi‘, ‘egon‘, ‘女神‘, ‘taibai‘, ‘lidongxu‘, ‘小‘, ‘明‘]
删
#pop 有返回值 li = [‘alex‘, ‘wusir‘, ‘liuyi‘, ‘egon‘, ‘女神‘, ‘taibai‘, ‘lidongxu‘, ‘小‘, ‘明‘] li.pop() #没有值 默认删除最后一个 print(li) #输出:[‘alex‘, ‘wusir‘, ‘liuyi‘, ‘egon‘, ‘女神‘, ‘taibai‘, ‘lidongxu‘, ‘小‘] #按照索引删除 li = [‘alex‘, ‘wusir‘, ‘liuyi‘, ‘egon‘, ‘女神‘, ‘taibai‘, ‘lidongxu‘, ‘小‘, ‘明‘] name = li.pop(0) #按照索引删除 print(name,li) #输出:alex [‘wusir‘, ‘liuyi‘, ‘egon‘, ‘女神‘, ‘taibai‘, ‘lidongxu‘, ‘小‘, ‘明‘] #remove 按照元素删除 无返回值 li = [‘alex‘, ‘wusir‘, ‘liuyi‘, ‘egon‘, ‘女神‘, ‘taibai‘, ‘lidongxu‘, ‘小‘, ‘明‘] li.remove(‘明‘) print(li) #clear 清空列表 li = [‘alex‘, ‘wusir‘, ‘liuyi‘, ‘egon‘, ‘女神‘, ‘taibai‘, ‘lidongxu‘, ‘小‘, ‘明‘] li.clear() print(li) #del删除列表 会报错 li = [‘alex‘, ‘wusir‘, ‘liuyi‘, ‘egon‘, ‘女神‘, ‘taibai‘, ‘lidongxu‘, ‘小‘, ‘明‘] del.li print(li) #切片删除列表中某一部分 li = [‘alex‘, ‘wusir‘, ‘liuyi‘, ‘egon‘, ‘女神‘, ‘taibai‘, ‘lidongxu‘, ‘小‘, ‘明‘] del li[0:3] print(li) #输出:[‘egon‘, ‘女神‘, ‘taibai‘, ‘lidongxu‘, ‘小‘, ‘明‘]
改
#按照索引修改 li = [‘alex‘, ‘wusir‘, ‘liuyi‘, ‘egon‘, ‘女神‘, ‘taibai‘, ‘lidongxu‘, ‘小‘, ‘明‘] li[0] = ‘lidongxu‘ #按照索引修改 print(li) #输出:[‘lidongxu‘, ‘wusir‘, ‘liuyi‘, ‘egon‘, ‘女神‘, ‘taibai‘, ‘lidongxu‘, ‘小‘, ‘明‘] #会拆分到最小单元 li = [‘alex‘, ‘wusir‘, ‘liuyi‘, ‘egon‘, ‘女神‘, ‘taibai‘, ‘lidongxu‘, ‘小‘, ‘明‘] li [0:2] = ‘你好啊‘ print(li) #输出:[‘你‘, ‘好‘, ‘啊‘, ‘liuyi‘, ‘egon‘, ‘女神‘, ‘taibai‘, ‘lidongxu‘, ‘小‘, ‘明‘] #完整替换切片部分 li = [‘alex‘, ‘wusir‘, ‘liuyi‘, ‘egon‘, ‘女神‘, ‘taibai‘, ‘lidongxu‘, ‘小‘, ‘明‘] li [0:2] = [‘1。2。3‘] print(li) #输出:[‘1。2。3‘, ‘liuyi‘, ‘egon‘, ‘女神‘, ‘taibai‘, ‘lidongxu‘, ‘小‘, ‘明‘]
查
for循环 #按照索引数循环 li = [‘alex‘, ‘wusir‘, ‘liuyi‘, ‘egon‘, ‘女神‘, ‘taibai‘, ‘lidongxu‘, ‘小‘, ‘明‘] for i in li : print(li) #输出: # [‘alex‘, ‘wusir‘, ‘liuyi‘, ‘egon‘, ‘女神‘, ‘taibai‘, ‘lidongxu‘, ‘小‘, ‘明‘] # [‘alex‘, ‘wusir‘, ‘liuyi‘, ‘egon‘, ‘女神‘, ‘taibai‘, ‘lidongxu‘, ‘小‘, ‘明‘] # [‘alex‘, ‘wusir‘, ‘liuyi‘, ‘egon‘, ‘女神‘, ‘taibai‘, ‘lidongxu‘, ‘小‘, ‘明‘] # [‘alex‘, ‘wusir‘, ‘liuyi‘, ‘egon‘, ‘女神‘, ‘taibai‘, ‘lidongxu‘, ‘小‘, ‘明‘] # [‘alex‘, ‘wusir‘, ‘liuyi‘, ‘egon‘, ‘女神‘, ‘taibai‘, ‘lidongxu‘, ‘小‘, ‘明‘] # [‘alex‘, ‘wusir‘, ‘liuyi‘, ‘egon‘, ‘女神‘, ‘taibai‘, ‘lidongxu‘, ‘小‘, ‘明‘] # [‘alex‘, ‘wusir‘, ‘liuyi‘, ‘egon‘, ‘女神‘, ‘taibai‘, ‘lidongxu‘, ‘小‘, ‘明‘] # [‘alex‘, ‘wusir‘, ‘liuyi‘, ‘egon‘, ‘女神‘, ‘taibai‘, ‘lidongxu‘, ‘小‘, ‘明‘] # [‘alex‘, ‘wusir‘, ‘liuyi‘, ‘egon‘, ‘女神‘, ‘taibai‘, ‘lidongxu‘, ‘小‘, ‘明‘] #按照索引数循环打印出切片内容 li = [‘alex‘, ‘wusir‘, ‘liuyi‘, ‘egon‘, ‘女神‘, ‘taibai‘, ‘lidongxu‘, ‘小‘, ‘明‘] for i in li : print(li[0:2]) # 输出: # [‘alex‘, ‘wusir‘] # [‘alex‘, ‘wusir‘] # [‘alex‘, ‘wusir‘] # [‘alex‘, ‘wusir‘] # [‘alex‘, ‘wusir‘] # [‘alex‘, ‘wusir‘] # [‘alex‘, ‘wusir‘] # [‘alex‘, ‘wusir‘] # [‘alex‘, ‘wusir‘] len #打印列表长度 li = [‘alex‘, ‘wusir‘, ‘liuyi‘, ‘egon‘, ‘女神‘, ‘taibai‘, ‘lidongxu‘, ‘小‘, ‘明‘] i = len(li) print(i) #输出:9 count #某一元素或字符串出现的次数 li = [‘alex‘, ‘wusir‘, ‘liuyi‘, ‘egon‘, ‘女神‘, ‘taibai‘, ‘lidongxu‘, ‘小‘, ‘明‘] i = li.count(‘alex‘) print(i) #输出:1 PS:列表中只能查引号中的元素。 index #索引位置,,没有会报错。列表中只能用index li = [‘alex‘, ‘wusir‘, ‘liuyi‘, ‘egon‘, ‘女神‘, ‘taibai‘, ‘lidongxu‘, ‘小‘, ‘明‘] print(li.index(‘wusir‘)) # 输出:1 sort #从小到大排序(升序) li = [‘1‘, ‘3‘, ‘2‘, ‘6‘, ‘4‘, ‘5‘, ‘7‘, ‘9‘, ‘8‘] li.sort() print(li) # # 输出:[‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘, ‘9‘] #从大到小排序(降序) li = [‘1‘, ‘3‘, ‘2‘, ‘6‘, ‘4‘, ‘5‘, ‘7‘, ‘9‘, ‘8‘] li.sort(reverse=True) print(li) # # 输出:[‘9‘, ‘8‘, ‘7‘, ‘6‘, ‘5‘, ‘4‘, ‘3‘, ‘2‘, ‘1‘] reverse #列表整体反转 li = [‘1‘, ‘3‘, ‘2‘, ‘6‘, ‘4‘, ‘5‘, ‘7‘, ‘9‘, ‘8‘] li.reverse() print(li) # # 输出:[‘8‘, ‘9‘, ‘7‘, ‘5‘, ‘4‘, ‘6‘, ‘2‘, ‘3‘, ‘1‘]
而且列表还能进行嵌套
#打印列表中小列表的元素 li = [‘alex‘, ‘wusir‘, ‘liuyi‘, ‘女神‘, ‘taibai‘, [‘8‘,‘3‘,‘lidongxu‘,‘1‘],‘小‘, ‘明‘] print(li[5][2]) # 输出:lidongxu #替换某个元素的首字母大写 li = [‘alex‘, ‘wusir‘, ‘liuyi‘, ‘女神‘, ‘taibai‘, [‘8‘,‘3‘,‘lidongxu‘,‘1‘],‘小‘, ‘明‘] li [0] = li[0].capitalize() print(li) # 输出:[‘Alex‘, ‘wusir‘, ‘liuyi‘, ‘女神‘, ‘taibai‘, [‘8‘, ‘3‘, ‘lidongxu‘, ‘1‘], ‘小‘, ‘明‘]
列表中的join方法:
# join()方法 # S.join():其中S是字符串连接符,join中参数必须是可迭代对象,join()方法输出可迭代对象的每一个元素,并用连接符将每一个元素连接 s = ‘ilovepython‘ l = [‘C‘,‘Java‘,‘C++‘,‘Ruby‘,‘PHP‘,‘Python‘] print(‘_‘.join(s)) >>> ‘i_l_o_v_e_p_y_t_h_o_n‘ print(‘_‘.join(l)) >>> ‘C_Java_C++_Ruby_PHP_Python‘
列表与str之间的转换:
# string <——> list : 字符串与列表的相互转换 # string ——> list : split() s = ‘i_l_o_v_e_p_y_t_h_o_n‘ print(s.split(‘_‘)) >>> [‘i‘, ‘l‘, ‘o‘, ‘v‘, ‘e‘, ‘p‘, ‘y‘, ‘t‘, ‘h‘, ‘o‘, ‘n‘] # list ——> string : join() l = [‘C‘,‘Java‘,‘C++‘,‘Ruby‘,‘PHP‘,‘Python‘] print(‘‘.join(l)) >>> CJavaC++RubyPHPPython
range()方法:
# range:相当于都是数字的列表 [0,1,2,3,4.....,99] # range(起始值,终止值) for i in range(0,100): print(i) # 默认起始值不写默认为0 for i in range(100): print(i) # 同索引一样,range也可以加步长,加步长的话默认的0起始值就不可以省略 for i in range(0,100,2): print(i) for i in range(100,0,-2): print(i) # ★★★注意!面试题:不报错,也不输出结果! ‘‘‘ 起始值:0 终止值:10 步长:-1 ‘‘‘ for i in range(0,10,-1): print(i)