前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python -- list 类

Python -- list 类

作者头像
py3study
发布2020-01-13 15:30:36
4030
发布2020-01-13 15:30:36
举报
文章被收录于专栏:python3
代码语言:javascript
复制
Python list类常用方法
class list(object):
    def append(self, p_object): # 向列表中添加元素;
 >>> name_list
 ['shuoming', 'python', 'search']
 >>> name_list.append("python")
 >>> name_list
 ['shuoming', 'python', 'search', 'python']
    def clear(self):       # 清除列表所有元素,清除后列表为空列表;
 >>> test_list
 ['shuoming', 'python', 'search', 'python', 'automatic', 1, 2]
 >>> test_list.clear()
 >>> test_list
 []
 >>>
    def copy(self):      
    def count(self, value):    # 列表中指定元素的个数;
 >>> name_list.count("python")
 2
    def extend(self, iterable):   把一个列表添加到另一个列表中;
 >>> a
 [1, 7, 2, 9, 3, 8, 'a', 'b']
 >>> b=[2,'f',3]
 >>> a+b
 [1, 7, 2, 9, 3, 8, 'a', 'b', 2, 'f', 3]
 >>> a.extend(b)
 >>> a
 [1, 7, 2, 9, 3, 8, 'a', 'b', 2, 'f', 3]
 >>> 
    def index(self, value, start=None, stop=None):   # 查列表中某一元素第一个索引值;
 >>> name_list.index("python")
 1
    def insert(self, index, p_object):   # 向列表中插入某一元素;
 >>> name_list.insert(3,"python")
 >>> name_list
 ['shuoming', 'python', 'search', 'python', 'python']
    def pop(self, index=None):   # 从列表最后删除元素;
 >>> last_one = name_list.pop()  
 >>> last_one
 'python'
 >>> name_list
 ['shuoming', 'python', 'search', 'python']
    def remove(self, value):   # 删除某一元素,默认是指定元素的第一个值;
 >>> name_list.remove('python')
 >>> name_list
 ['shuoming', 'search', 'python']
    def reverse(self):      #  翻转;
 >>> name_list
 ['shuoming', 'search', 'python', '@', '$']
 >>> name_list.reverse()
 >>> name_list
 ['$', '@', 'python', 'search', 'shuoming']
    def sort(self, key=None, reverse=False):   # 排序,特殊字符在前;
 >>> name_list
 ['shuoming', 'search', 'python',  '@', '$']
 >>> name_list.sort()
 >>> print (name_list)
 ['$', '@', 'python', 'search', 'shuoming']
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/08/13 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档