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

【Python】06、python内置数据结构1

时间:2017-05-28 23:49:37      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:python   list   


一、数据结构与获取帮助信息

1、数据结构

  通过某种方式(例如对元素进行编号)组织在一起的数据元素的集合,这些数据元素可以是数字或者字符,甚至可以是其它的数据结构。

  python的最基本数据结构是序列

  序列中的每个元素被分配一个序号(即元素的位置),也称为索引:索引从0开始编号


2、python中如何获取命令帮助

获取对象支持使用的属性和方法:dir()

某方法的具体使用帮助:help()

获取可调用对象的文档字串:print(obj.__doc__)

In [15]: dir(list)
Out[15]: 
[‘__add__‘,
 ‘__class__‘,
 ‘__contains__‘,
 ‘__delattr__‘,
 ‘__delitem__‘,
 ‘__dir__‘,
 ‘__doc__‘,
 ‘__eq__‘,
 ‘__format__‘,
 ‘__ge__‘,
 ‘__getattribute__‘,
 ‘__getitem__‘,
 ‘__gt__‘,
 ‘__hash__‘,
 ‘__iadd__‘,
 ‘__imul__‘,
 ‘__init__‘,
 ‘__init_subclass__‘,
 ‘__iter__‘,
 ‘__le__‘,
 ‘__len__‘,
 ‘__lt__‘,
 ‘__mul__‘,
 ‘__ne__‘,
 ‘__new__‘,
 ‘__reduce__‘,
 ‘__reduce_ex__‘,
 ‘__repr__‘,
 ‘__reversed__‘,
 ‘__rmul__‘,
 ‘__setattr__‘,
 ‘__setitem__‘,
 ‘__sizeof__‘,
 ‘__str__‘,
 ‘__subclasshook__‘,
 ‘append‘,
 ‘clear‘,
 ‘copy‘,
 ‘count‘,
 ‘extend‘,
 ‘index‘,
 ‘insert‘,
 ‘pop‘,
 ‘remove‘,
 ‘reverse‘,
 ‘sort‘]
 
 In [17]: help(list)

Help on list object:

class list(object)
 |  list() -> new empty list
 |  list(iterable) -> new list initialized from iterable‘s items
 |  
 |  Methods defined here:
 |  
 |  __add__(self, value, /)
 |      Return self+value.
 |  
 |  __contains__(self, key, /)
 |      Return key in self.
 |  
 |  __delitem__(self, key, /)
 |      Delete self[key].
 |  
 |  __eq__(self, value, /)
 |      Return self==value.
 |  
 |  __ge__(self, value, /)
 |      Return self>=value.
 
 
In [20]: print(list.__doc__)
list() -> new empty list
list(iterable) -> new list initialized from iterable‘s items

In [21]: list.__doc__
Out[21]: "list() -> new empty list\nlist(iterable) -> new list initialized from iterable‘s items"


二、列表

1、列表

列表是一个任意类型的对象的位置相关的有序集合。

          列表是一个序列,用于顺序的存储数据

列表的定义和初始化:

In [5]: lst1 = list()      # 使用工厂函数list()

In [6]: lst2 = []          # 使用[]

In [7]: type(lst1)
Out[7]: list

In [8]: type(lst2)
Out[8]: list

In [9]: lst1 = list(range(10))   # 将一个可迭代对象转化为列表

In [10]: lst1
Out[10]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

     通常在定义列表的时候使用中括号,在转化可迭代对象为列表时用list()


三、列表相关的操作

      对列表一般有增、删、改、查的相关操作

1、查

 1)通过索引(下标)访问列表的元素

      返回该索引对应的元素 

      索引从左边开始,从0开始,不能超出范围,否则抛出IndexError

      负数索引从右边开始,从-1开始

In [25]: lst1
Out[25]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

In [26]: lst1[0]
Out[26]: 0

In [27]: lst1[10]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-27-255d55760a91> in <module>()
----> 1 lst1[10]

IndexError: list index out of range

In [28]: lst1[-1]
Out[28]: 9

In [29]: lst1[-3]
Out[29]: 7

In [30]: lst1[-3]


2)list.index()

      返回查找到该元素的第一个索引

      如果该元素不存在,则抛出ValueError

      start参数指定从哪个索引开始查找;stop参数指定从哪个索引结束,并且不包含该索引

      start和stop可以为负数,但是总是从左往右查找

In [51]: help(lst2.index)

Help on built-in function index:

index(...) method of builtins.list instance
    L.index(value, [start, [stop]]) -> integer -- return first index of value.
    Raises ValueError if the value is not present.
    
In [47]: lst2=[1, 3, 5, 2, 3, 5]

In [48]: lst2.index(3)
Out[48]: 1

In [49]: lst2.index(2)
Out[49]: 3

In [52]: lst2.index(8)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-52-857c8a1f260a> in <module>()
----> 1 lst2.index(8)

ValueError: 8 is not in list


In [56]: lst2.index(3, 3)
Out[56]: 4

In [57]: lst2.index(3, 3, 4)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-57-dd5e9d56cf7c> in <module>()
----> 1 lst2.index(3, 3, 4)

ValueError: 3 is not in list

In [59]: lst2.index(3, 3, 5)
Out[59]: 4

In [60]: lst2.index(3, 4, 5)
Out[60]: 4

In [70]: lst2.index(3, -1, -6,)   # start 大于 stop 是一个空列表    
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-70-b3ae59853639> in <module>()
----> 1 lst2.index(3, -1, -6,)

ValueError: 3 is not in list

In [71]: lst2.index(3, -6, -1,)
Out[71]: 1

In [74]: lst2.index(3, -6, 9,)

In [98]: lst2.index(3, 1, 1)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-98-a95f8fe9908b> in <module>()
----> 1 lst2.index(3, 1, 1)

ValueError: 3 is not in list

In [99]: lst2.index(3, 1, 2)
Out[99]: 1


list.index()函数的实现原型:

def index(lst, value, start = 0, stop = -1):
    i = start
    for x in lst[start: end]
        if x == value:
            return i
        i += 1
    rais ValueError()


 3)list.count()

     返回该值在列表中出现的次数

In [89]: lst2
Out[89]: [1, 3, 5, 2, 3, 5]

In [90]: lst2.count(1)
Out[90]: 1

In [91]: lst2.count(5)
Out[91]: 2

In [92]: lst2.count(8)
Out[92]: 0


原型:

def count(lst, value):
    c = 0
    for x in lst:
        if x == value:
            c += 1
    return c



小结:

    index()和count()的时间复杂度是O(n),也称线性复杂度;效率与数据规模线性相关





【Python】06、python内置数据结构1

标签:python   list   

原文地址:http://xiexiaojun.blog.51cto.com/2305291/1930506

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