码迷,mamicode.com
首页 > 编程语言 > 详细

Python -- list 类

时间:2016-01-12 15:41:55      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:python   list   count   reverse   pop   


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‘]



本文出自 “纷繁中享受技术的简单喜悦” 博客,请务必保留此出处http://51enjoy.blog.51cto.com/8393791/1734138

Python -- list 类

标签:python   list   count   reverse   pop   

原文地址:http://51enjoy.blog.51cto.com/8393791/1734138

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