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

python day2 列表 元组 字典 字符串

时间:2016-01-23 23:07:09      阅读:302      评论:0      收藏:0      [点我收藏+]

标签:

列表

#列表事例

>>> li = list((1,2,3,4,5))
>>> print li
[1, 2, 3, 4, 5]


>>> li2 = [‘a‘,‘b‘,‘c‘,‘d‘]
>>> print li2
[‘a‘, ‘b‘, ‘c‘, ‘d‘]
>>>

#列表尾部增加元素
>>> li.append(6)
>>> li
[1, 2, 3, 4, 5, 6]


#清空列表内的元素,适用于py3
li.clear()
print(li)
[]


#列表的拷贝(深浅拷贝)(忽略)
li = [1,2,3,4,]
li3 = li.copy()
li3.append(678,)
print(li)
print(li3)
liid = id(li)
liid3 = id(li3)
print(liid,liid3)

[1, 2, 3, 4]
[1, 2, 3, 4, 678]
35707080 35737992

 

#extend合并两个列表或者把列表和元组合并
>>> li.extend([1,2])
>>> li
[1, 2, 3, 4, 5, 6, 1, 2]

 

#count判断某个元素出现的次数
>>> li.count(1)
2

 


#index获取列表内元素的索引下表
>>> li.index(6)
5


#insert指定下标,插入元素
>>> li.insert(2,‘abc‘)
>>> li
[1, 2, ‘abc‘, 3, 4, 5, 6, 1, 2]

 

#pop移除某一项,加下标
>>> li
[1, 2, ‘abc‘, 3, 4, 5, 6, 1, 2]
>>> res = li.pop(3)
>>> print res
3
>>> print li
[1, 2, ‘abc‘, 4, 5, 6, 1, 2]

 

#remove删除--pop   加值,pop是加下标
>>> li.remove(‘abc‘)
>>> li
[1, 2, 4, 5, 6, 1, 2]

 


#reverse反向排序
>>> li
[1, 2, 4, 5, 6, 1, 2]
>>> li.reverse()
>>> li
[2, 1, 6, 5, 4, 2, 1]

 

#sort排序
>>> li
[2, 1, 6, 5, 4, 2, 1]
>>> li.sort()
>>> li
[1, 1, 2, 2, 4, 5, 6]
>>>

英文介绍

技术分享
class list(object):
    """
    list() -> new empty list
    list(iterable) -> new list initialized from iterable‘s items
    """
    def append(self, p_object): # real signature unknown; restored from __doc__
        """ L.append(object) -> None -- append object to end """
        pass

    def clear(self): # real signature unknown; restored from __doc__
        """ L.clear() -> None -- remove all items from L """
        pass

    def copy(self): # real signature unknown; restored from __doc__
        """ L.copy() -> list -- a shallow copy of L """
        return []

    def count(self, value): # real signature unknown; restored from __doc__
        """ L.count(value) -> integer -- return number of occurrences of value """
        return 0

    def extend(self, iterable): # real signature unknown; restored from __doc__
        """ L.extend(iterable) -> None -- extend list by appending elements from the iterable """
        pass

    def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__
        """
        L.index(value, [start, [stop]]) -> integer -- return first index of value.
        Raises ValueError if the value is not present.
        """
        return 0

    def insert(self, index, p_object): # real signature unknown; restored from __doc__
        """ L.insert(index, object) -- insert object before index """
        pass

    def pop(self, index=None): # real signature unknown; restored from __doc__
        """
        L.pop([index]) -> item -- remove and return item at index (default last).
        Raises IndexError if list is empty or index is out of range.
        """
        pass

    def remove(self, value): # real signature unknown; restored from __doc__
        """
        L.remove(value) -> None -- remove first occurrence of value.
        Raises ValueError if the value is not present.
        """
        pass

    def reverse(self): # real signature unknown; restored from __doc__
        """ L.reverse() -- reverse *IN PLACE* """
        pass

    def sort(self, key=None, reverse=False): # real signature unknown; restored from __doc__
        """ L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE* """
        pass
View Code


字典

技术分享

 

明天续~~

python day2 列表 元组 字典 字符串

标签:

原文地址:http://www.cnblogs.com/dribs/p/5154258.html

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