大蛇02 列表操作练习
Posted max404
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了大蛇02 列表操作练习相关的知识,希望对你有一定的参考价值。
# 1.append
# 功能:在列表结尾追加元素(追加一个)
# Ex.
# Marvel_Heros = [‘Mad Man‘,‘Mad Man‘,‘Super Man‘]
# Marvel_Heros.append(‘X-Man‘)
# print(Marvel_Heros)
# 回车:[‘Mad Man‘, ‘Mad Man‘, ‘Super Man‘, ‘X-Man‘]
# 2.clean
# 功能:清空列表
# Ex.
# Marvel_Heros = [‘Mad Man‘,‘Mad Man‘,‘Super Man‘]
# Marvel_Heros.clear()
# print(Marvel_Heros)
# 回车:[]
# 3.copy
# 功能:列表拷贝
# Ex.
# Name = [‘Mad Man‘, ‘Mad Man‘, ‘Super Man‘, ‘X-Man‘]
# Marvel_Heros = [‘Max‘,‘Ace‘]
# Marvel_Heros = Name.copy()
# print(Marvel_Heros)
# 回车:[‘Mad Man‘, ‘Mad Man‘, ‘Super Man‘, ‘X-Man‘]
# 4.count
# 功能:统计列表中指定元素
# Ex1.
# Name = [‘Mad Man‘, ‘Mad Man‘, ‘Super Man‘, ‘X-Man‘]
# Marvel_Heros = Name.append(‘X-Man‘)
# Count_X_Man = Name.count(‘X-Man‘)
# print(Count_X_Man)
# 回车:2
# Ex2.只统计最外层元素个数
# Name = [‘Mad Man‘, [‘X-Man‘],‘Mad Man‘, ‘Super Man‘, [‘X-Man‘]]
# Marvel_Heros = Name.append(‘X-Man‘)
# Count_X_Man = Name.count(‘X-Man‘)
# print(Count_X_Man)
# 回车:1
# 5.extend
# 功能:追加字符元素或者列表元素
# Ex:
# Name = [‘Mad Man‘, ‘Mad Man‘, ‘Super Man‘, ‘X-Man‘]
# Marvel_Heros = [‘Marvel‘,‘Spider Man‘]
# Str_1 = ‘123‘
# Str_2 = ‘A‘
# Name.extend(Str_1) 字符串 拆分 添加
# print(Name)
# 回车:[‘Mad Man‘, ‘Mad Man‘, ‘Super Man‘, ‘X-Man‘, ‘1‘, ‘2‘, ‘3‘]
# Name.extend(Str_2)
# print(Name)
# 回车:[‘Mad Man‘, ‘Mad Man‘, ‘Super Man‘, ‘X-Man‘, ‘1‘, ‘2‘, ‘3‘, ‘A‘]
# Name.extend(Marvel_Heros)
# print(Name)
# 回车:[‘Mad Man‘, ‘Mad Man‘, ‘Super Man‘, ‘X-Man‘, ‘1‘, ‘2‘, ‘3‘, ‘A‘, ‘Marvel‘, ‘Spider Man‘]
# 6.index
# 功能:定位列表中某元素,返回索引,如果找到会直接报错
# Ex.
# name = [‘sd‘, ‘dfdf‘, ‘drer‘, [‘sddss‘, ‘sdsdsd‘]]
# A = name.index(‘sd‘)
# print(A)
# 回车:0
# 7.insert
# 功能:在指定索引位置的元素前面插入新的元素
# Ex.
# name = [‘sd‘, ‘dfdf‘, ‘drer‘, [‘sddss‘, ‘sdsdsd‘]]
# name.insert(1,‘asd‘)
# print(name)
# 回车:[‘sd‘, ‘asd‘, ‘dfdf‘, ‘drer‘, [‘sddss‘, ‘sdsdsd‘]]
# 8.pop
# 功能:删除指定索引值的元素,返回值为当前删除的元素的值。不指定索引值,默认删除最后一个元素
# Ex.
# name = [‘sd‘, ‘dfdf‘, ‘drer‘, [‘sddss‘, ‘sdsdsd‘]]
# name.pop(2)
# print(name)
# 回车:[‘sd‘, ‘dfdf‘, [‘sddss‘, ‘sdsdsd‘]]
# 9.remove
# 功能:删除指定元素
# Ex.
# name = [‘sd‘, ‘dfdf‘, ‘drer‘, [‘sddss‘, ‘sdsdsd‘]]
# name.remove(‘drer‘)
# print(name)
# 回车:[‘sd‘, ‘dfdf‘, [‘sddss‘, ‘sdsdsd‘]]
# 10.reverse
# 功能:用于反向列表中元素
# Ex.
# name = [‘sd‘, ‘dfdf‘, ‘drer‘, [‘sddss‘, ‘sdsdsd‘]]
# name.reverse()
# print(name)
# 回车:[[‘sddss‘, ‘sdsdsd‘], ‘drer‘, ‘dfdf‘, ‘sd‘]
# 11.sort
# 功能:同级同类排序,对单层列表进行元素的排序
# Ex.
# name = [‘sd‘, ‘dfdf‘, ‘drerasd‘]
# name.sort(key=len, reverse=False)
# print(name)
# 回车:[‘sd‘, ‘dfdf‘, ‘drerasd‘]
# name = [‘sd‘, ‘dfdf‘, ‘drerasd‘]
# name.sort(key=len, reverse=True)
# print(name)
# 回车:[‘drerasd‘, ‘dfdf‘, ‘sd‘]
以上是关于大蛇02 列表操作练习的主要内容,如果未能解决你的问题,请参考以下文章