Python基础之列表功能
今天记录一下列表的基础功能:
定义一个列表: >>> l1=[1,2,3] >>> l2=list([1,2,3,]) >>> l1 [1, 2, 3] >>> l2 [1, 2, 3] 1、append: 像列表中添加一个元素 >>> l1=[1,2,3,] >>> l1.append(4) >>> l1 [1, 2, 3, 4] 2、clear: 清空列表 >>> l1=['chai','zhen','hua',] >>> l1.clear() >>> l1 [] 3、count: 统计指定元素在列表中出现的次数 >>> l1=['chai','zhen','hua',] >>> l1.count('chai') 1 4、extend: 扩展列表,只能添加一个列表 >>> l1=['chai','zhen','hua',] >>> l2=[1,2,3,] >>> l1.extend(l2) >>> l1 ['chai', 'zhen', 'hua', 1, 2, 3] 5、index: 返回指定元素在列表中的位置 >>> l1=['chai','zhen','hua',] >>> l1.index('hua') 2 6、insert: 指定位置在列表中插入元素 >>> l1=['chai','zhen','hua',] >>> l1.insert(3,'chai') >>> l1 ['chai', 'zhen', 'hua', 'chai'] 7、pop: 从列表中拿出最后一个元素 >>> ret=l1.pop() >>> ret 'chai' >>> l1 ['chai', 'zhen', 'hua'] >>> ret=l1.pop() >>> l1 ['chai', 'zhen'] 8、remove: 删除指定的列表元素,与pop不同的是它不返回删除的值 >>> l1=['chai','zhen','hua',] >>> ret=l1.remove('zhen') >>> print(ret) None >>> l1 ['chai', 'hua'] 9、reverse: 翻转列表 >>> l1=[1,2,3,] >>> l1.reverse() >>> l1 [3, 2, 1] 10、sort: 对列表排序 >>> l1=['c','k','ss','p',] >>> l1 ['c', 'k', 'ss', 'p'] >>> l1.sort() >>> l1 ['c', 'k', 'p', 'ss']
相关推荐
坚持是一种品质 2020-11-16
zhaobig 2020-11-16
文山羊 2020-10-31
kikaylee 2020-10-31
oXiaoChong 2020-10-30
PYTandFA 2020-09-15
CloudStrife0 2020-08-23
hang0 2020-08-16
lzujiangge 2020-08-16
RocNg 2020-08-15
xiesheng 2020-08-06
Tristahong 2020-08-05
georgeandgeorge 2020-08-03
elizabethxxy 2020-07-27
flycony 2020-07-05
柠檬班 2020-07-04
magicandy 2020-07-04
fanhuasijin 2020-06-27