python基础知识-列表,元组,字典
生活随笔
收集整理的這篇文章主要介紹了
python基础知识-列表,元组,字典
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
列表(list)
賦值方法:
?l = [11,45,67,34,89,23]
l = list()
列表的方法:
1 #!/usr/bin/env python 2 3 class list(object): 4 """ 5 list() -> new empty list 6 list(iterable) -> new list initialized from iterable's items 7 """ 8 def append(self, p_object): # real signature unknown; restored from __doc__ 9 '''在列表末尾添加一個(gè)新的對象''' 10 """ L.append(object) -> None -- append object to end """ 11 pass 12 13 def clear(self): # real signature unknown; restored from __doc__ 14 '''清空列表中的所有對象''' 15 """ L.clear() -> None -- remove all items from L """ 16 pass 17 18 def copy(self): # real signature unknown; restored from __doc__ 19 '''拷貝一個(gè)新的列表''' 20 """ L.copy() -> list -- a shallow copy of L """ 21 return [] 22 23 def count(self, value): # real signature unknown; restored from __doc__ 24 '''某個(gè)元素在列表中出現(xiàn)的次數(shù)''' 25 """ L.count(value) -> integer -- return number of occurrences of value """ 26 return 0 27 28 def extend(self, iterable): # real signature unknown; restored from __doc__ 29 '''在列表的末尾追加另外一個(gè)列表的多個(gè)值''' 30 """ L.extend(iterable) -> None -- extend list by appending elements from the iterable """ 31 pass 32 33 def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__ 34 '''查找給定值第一次出現(xiàn)的位置''' 35 """ 36 L.index(value, [start, [stop]]) -> integer -- return first index of value. 37 Raises ValueError if the value is not present. 38 """ 39 return 0 40 41 def insert(self, index, p_object): # real signature unknown; restored from __doc__ 42 '''指定位置插入元素''' 43 """ L.insert(index, object) -- insert object before index """ 44 pass 45 46 def pop(self, index=None): # real signature unknown; restored from __doc__ 47 '''移除列表中最后一個(gè)元素,并獲取這個(gè)元素''' 48 """ 49 L.pop([index]) -> item -- remove and return item at index (default last). 50 Raises IndexError if list is empty or index is out of range. 51 """ 52 pass 53 54 def remove(self, value): # real signature unknown; restored from __doc__ 55 '''移除列表中給定值的第一次出現(xiàn)的元素''' 56 """ 57 L.remove(value) -> None -- remove first occurrence of value. 58 Raises ValueError if the value is not present. 59 """ 60 pass 61 62 def reverse(self): # real signature unknown; restored from __doc__ 63 '''反轉(zhuǎn)列表''' 64 """ L.reverse() -- reverse *IN PLACE* """ 65 pass 66 67 def sort(self, key=None, reverse=False): # real signature unknown; restored from __doc__ 68 '''對列表中的元素排序''' 69 """ L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE* """ 70 pass list方法示例:
#### append() >>>?l?=?[11,45,67,34,89,23] >>>?l.append(44) >>>?l [11,?45,?67,?34,?89,?23,?44] #### >>>?l [1,?4,?7,?11,?23,?34,?34,?44,?44,?45,?67,?89] >>>?l.clear() >>>?l [] #### copy() >>>?l [11,?45,?67,?34,?89,?23,?44] >>>?i?=?l.copy() >>>?i [11,?45,?67,?34,?89,?23,?44] #### count() >>>?l [11,?45,?67,?34,?89,?23,?44,?44,?44,?45,?34] >>>?l.count(44) 3 #### extend() >>>?i?=?[1,4,7,6] >>>?l [11,?45,?67,?34,?89,?23,?44,?44,?44,?45,?34] >>>?l.extend(i) >>>?l [11,?45,?67,?34,?89,?23,?44,?44,?44,?45,?34,?1,?4,?7,?6] #### indexi() >>>?l [11,?45,?67,?34,?89,?23,?44,?44,?44,?45,?34,?1,?4,?7,?6] >>>?l.index(44) 6 >>>?l.index(45) 1 #### pop() >>>?l [11,?45,?67,?34,?89,?23,?44,?44,?44,?45,?34,?1,?4,?7,?6] >>>?l.pop() 6 >>>?l [11,?45,?67,?34,?89,?23,?44,?44,?44,?45,?34,?1,?4,?7] #### remove() >>>?l [11,?45,?67,?34,?89,?23,?44,?44,?44,?45,?34,?1,?4,?7] >>>?l.remove(45) >>>?l [11,?67,?34,?89,?23,?44,?44,?44,?45,?34,?1,?4,?7] >>>?l.remove(44) >>>?l [11,?67,?34,?89,?23,?44,?44,?45,?34,?1,?4,?7] #### reverse() >>>?l [11,?67,?34,?89,?23,?44,?44,?45,?34,?1,?4,?7] >>>?l.reverse() >>>?l [7,?4,?1,?34,?45,?44,?44,?23,?89,?34,?67,?11] #### sort() >>>?l [7,?4,?1,?34,?45,?44,?44,?23,?89,?34,?67,?11] >>>?l.sort() >>>?l [1,?4,?7,?11,?23,?34,?34,?44,?44,?45,?67,?89] #### 元組: 元組中的元素是不可以改變的。 賦值方法: tup?=?'a','b','c' tup = ('a',?'b',?'c')?元組的方法:
1 #!/usr/bin/env python 2 class tuple(object): 3 """ 4 tuple() -> empty tuple 5 tuple(iterable) -> tuple initialized from iterable's items 6 7 If the argument is a tuple, the return value is the same object. 8 """ 9 def count(self, value): # real signature unknown; restored from __doc__ 10 '''某個(gè)元素在元素中出現(xiàn)的次數(shù)''' 11 """ T.count(value) -> integer -- return number of occurrences of value """ 12 return 0 13 14 def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__ 15 '''查找給定值第一次出現(xiàn)的位置''' 16 """ 17 T.index(value, [start, [stop]]) -> integer -- return first index of value. 18 Raises ValueError if the value is not present. 19 """ 20 return 0 21 22 def __add__(self, *args, **kwargs): # real signature unknown 23 """ Return self+value. """ 24 pass 25 26 def __contains__(self, *args, **kwargs): # real signature unknown 27 """ Return key in self. """ 28 pass 29 30 def __eq__(self, *args, **kwargs): # real signature unknown 31 """ Return self==value. """ 32 pass 33 34 def __getattribute__(self, *args, **kwargs): # real signature unknown 35 """ Return getattr(self, name). """ 36 pass 37 38 def __getitem__(self, *args, **kwargs): # real signature unknown 39 """ Return self[key]. """ 40 pass 41 42 def __getnewargs__(self, *args, **kwargs): # real signature unknown 43 pass 44 45 def __ge__(self, *args, **kwargs): # real signature unknown 46 """ Return self>=value. """ 47 pass 48 49 def __gt__(self, *args, **kwargs): # real signature unknown 50 """ Return self>value. """ 51 pass 52 53 def __hash__(self, *args, **kwargs): # real signature unknown 54 """ Return hash(self). """ 55 pass 56 57 def __init__(self, seq=()): # known special case of tuple.__init__ 58 """ 59 tuple() -> empty tuple 60 tuple(iterable) -> tuple initialized from iterable's items 61 62 If the argument is a tuple, the return value is the same object. 63 # (copied from class doc) 64 """ 65 pass 66 67 def __iter__(self, *args, **kwargs): # real signature unknown 68 """ Implement iter(self). """ 69 pass 70 71 def __len__(self, *args, **kwargs): # real signature unknown 72 """ Return len(self). """ 73 pass 74 75 def __le__(self, *args, **kwargs): # real signature unknown 76 """ Return self<=value. """ 77 pass 78 79 def __lt__(self, *args, **kwargs): # real signature unknown 80 """ Return self<value. """ 81 pass 82 83 def __mul__(self, *args, **kwargs): # real signature unknown 84 """ Return self*value.n """ 85 pass 86 87 @staticmethod # known case of __new__ 88 def __new__(*args, **kwargs): # real signature unknown 89 """ Create and return a new object. See help(type) for accurate signature. """ 90 pass 91 92 def __ne__(self, *args, **kwargs): # real signature unknown 93 """ Return self!=value. """ 94 pass 95 96 def __repr__(self, *args, **kwargs): # real signature unknown 97 """ Return repr(self). """ 98 pass 99 100 def __rmul__(self, *args, **kwargs): # real signature unknown 101 """ Return self*value. """ 102 pass tuple方法示例:
####
count()
>>> tup = ('a','b','c','b')
>>> tup.count('b')
2
>>> tup.index('b')
1
?
方法示例:
#### clear() >>>?dic {'k1':?'v1',?'k2':?'v2',?'k3':?'v3'} >>>?dic.clear() >>>?dic {} #### copy() >>>?dic?=?{'k1':'v1','k2':'v2','k3':'v3'} >>>?dic2?=?dic.copy() >>>?dic2 {'k1':?'v1',?'k2':?'v2',?'k3':?'v3'} #### >>>?l?=?[2,3,5,6,7] >>>?d?=?dict.fromkeys(l) >>>?d {2:?None,?3:?None,?5:?None,?6:?None,?7:?None} >>>?d?=?dict.fromkeys(l,'a') >>>?d {2:?'a',?3:?'a',?5:?'a',?6:?'a',?7:?'a'} #### items() >>>?dic?=?{'k1':'v1','k2':'v2','k3':'v3'} >>>?dic.items() dict_items([('k1',?'v1'),?('k2',?'v2'),?('k3',?'v3')]) #### keys() >>>?dic?=?{'k1':'v1','k2':'v2','k3':'v3'} >>>?dic.keys() dict_keys(['k1',?'k2',?'k3']) #### pop() >>>?dic?=?{'k1':'v1','k2':'v2','k3':'v3'} >>>?dic.pop('k2') 'v2' >>>?dic {'k1':?'v1',?'k3':?'v3'} #### popitme() >>>?dic?=?{'k1':'v1','k2':'v2','k3':'v3'} >>>?dic.popitem() ('k2',?'v2') >>>?dic {'k1':'v1','k3':'v3'} #### setdefault() >>>?dic?=?{'k1':'v1','k2':'v2','k3':'v3'} >>>?dic.setdefault('k2') 'v2' >>>?dic.setdefault('k4','v4') 'v4' >>>?dic {'k1':?'v1',?'k4':?'v4',?'k2':?'v2',?'k3':?'v3'} #### update() >>>?dic {'k1':?'v1',?'k4':?'v4',?'k2':?'v2',?'k3':?'v3'} >>>?dic2 {'k5':?'v5'} >>>?dic.update(dic2) >>>?dic {'k1':?'v1',?'k5':?'v5',?'k4':?'v4',?'k2':?'v2',?'k3':?'v3'} >>>?dic2 {'k5':?'v5'} #### values() >>>?dic {'k1':?'v1',?'k2':?'v2',?'k3':?'v3'} >>>?dic.values() dict_values(['v1',?'v2',?'v3']) ####轉(zhuǎn)載于:https://www.cnblogs.com/binges/p/5118998.html
總結(jié)
以上是生活随笔為你收集整理的python基础知识-列表,元组,字典的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: AMD服务器CPU翻身:堪比20年前赢得
- 下一篇: h5engine造轮子