码迷,mamicode.com
首页 > 其他好文 > 详细

大蛇02 列表操作练习

时间:2019-03-24 09:33:28      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:int   append   拆分   ros   默认   字符串   clean   key   asd   

# 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 列表操作练习

标签:int   append   拆分   ros   默认   字符串   clean   key   asd   

原文地址:https://www.cnblogs.com/max404/p/10586606.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!