python基础之列表跟字典

列表

 1 #列表语法
 2 name = ["xiaozhang","xiaohan","xiaoqu",22,33,444,666,111,"QQ","微信","xiaoma"]
 3 name2 = ["uiqq","quxie","tiqi"]
 4 #增:
 5 name.append("ty")#追加到最后一行
 6 name.insert(3,"tu")#插入,前面写所插入的下标,后面写所插入的字符串
 7 
 8 #
 9 del name["xiaozhang"]#删除指定
10 del name #删除整个列表
11 name.remove("xiaohan")#删除指定
12 name.pop()#默认删除最后一个
13 
14 #
15 name[1] = "sana"#更改值
16 
17 #
18 name[1]#取第一个值
19 name[1:4]#取第一个到第三个值
20 name[1:5:2]#取第一个跟第4个值每隔一个取一个
21 name.index()#查找并返回下标
22 len(name)#查看长度
23 
24 #其他:
25 name.count()#计数
26 name.extend(name2)#扩展,如果有重复值不会代替。
27 name.reverse()#反转
28 
29 for i in range(name.count(33)):
30     ele_index = name.index(33)
31     name[ele_index] =999
32 
33 class list(object):#列表的属性
34     """
35     list() -> new empty list
36     list(iterable) -> new list initialized from iterable's items
37     """
38     def append(self, p_object): # real signature unknown; restored from __doc__#追加到最后面
39         """ L.append(object) -> None -- append object to end """
40         pass
41 
42     def clear(self): # real signature unknown; restored from __doc__#清除
43         """ L.clear() -> None -- remove all items from L """
44         pass
45 
46     def copy(self): # real signature unknown; restored from __doc__#复制
47         """ L.copy() -> list -- a shallow copy of L """
48         return []
49 
50     def count(self, value): # real signature unknown; restored from __doc__#计数
51         """ L.count(value) -> integer -- return number of occurrences of value """
52         return 0
53 
54     def extend(self, iterable): # real signature unknown; restored from __doc__
55         """ L.extend(iterable) -> None -- extend list by appending elements from the iterable """#扩展
56         pass
57 
58     def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__#获取下标
59         """
60         L.index(value, [start, [stop]]) -> integer -- return first index of value.
61         Raises ValueError if the value is not present.
62         """
63         return 0
64 
65     def insert(self, index, p_object): # real signature unknown; restored from __doc__#插入
66         """ L.insert(index, object) -- insert object before index """
67         pass
68 
69     def pop(self, index=None): # real signature unknown; restored from __doc__#默认删除最后一个
70         """
71         L.pop([index]) -> item -- remove and return item at index (default last).
72         Raises IndexError if list is empty or index is out of range.
73         """
74         pass
75 
76     def remove(self, value): # real signature unknown; restored from __doc__#指定删除
77         """
78         L.remove(value) -> None -- remove first occurrence of value.
79         Raises ValueError if the value is not present.
80         """
81         pass
82 
83     def reverse(self): # real signature unknown; restored from __doc__#反转
84         """ L.reverse() -- reverse *IN PLACE* """
85         pass
86 
87     def sort(self, key=None, reverse=False): # real signature unknown; restored from __doc__#排序
88         """ L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE* """
89         pass

字典

  1 #语法
  2 dic= {"name":"alex","age":33,"job":"IT"}#结构k:v,k:v
  3 dic1 = {"tya":"ret","set":2453}
  4 #查:
  5 dic["name"]
  6 dic.get('name')
  7 
  8 #添加:
  9 dic['gender'] = 'female'
 10 
 11 #改:
 12 dic["name"] = "laf"
 13 
 14 #删:
 15 del dic["name"]
 16 
 17 #其他
 18 dic.update(dic1)#更新字典,去重
 19 dic.items()#转换成列表
 20 dic.values()#打印所有values值
 21 dic.keys()#打印所有的key值
 22 
 23 print("name" in dic)#判断key是否在字典里面返回True or Flase
 24 dic.setdefault("ct")#存在返回值,不存在自动生成k,v
 25 dic.fromkeys([1,2,3,4,5,6],'ddd')#把列表里面的数字当做key,后面当成v
 26 for key in dic:
 27     print(key,dic[key])
 28 
 29 #字典里面套字典
 30 id_db = {
 31     371481199306143632:{
 32         'name':'xiaoli',
 33         'age':22,
 34         'addr':'shandong'
 35     },
 36     371481199306143633:{
 37         'name':'xiaoly',
 38         'age':22,
 39         'addr':'shandong'
 40     }
 41 }
 42 
 43 class dict(object):
 44     """
 45     dict() -> new empty dictionary
 46     dict(mapping) -> new dictionary initialized from a mapping object's
 47         (key, value) pairs
 48     dict(iterable) -> new dictionary initialized as if via:
 49         d = {}
 50         for k, v in iterable:
 51             d[k] = v
 52     dict(**kwargs) -> new dictionary initialized with the name=value pairs#成对出现
 53         in the keyword argument list.  For example:  dict(one=1, two=2)
 54     """
 55     def clear(self): # real signature unknown; restored from __doc__#清除
 56         """ D.clear() -> None.  Remove all items from D. """
 57         pass
 58 
 59     def copy(self): # real signature unknown; restored from __doc__#复制
 60         """ D.copy() -> a shallow copy of D """
 61         pass
 62 
 63     @staticmethod # known case
 64     def fromkeys(*args, **kwargs): # real signature unknown
 65         """ Returns a new dict with keys from iterable and values equal to value. """
 66         pass
 67 
 68     def get(self, k, d=None): # real signature unknown; restored from __doc__#得到值
 69         """ D.get(k[,d]) -> D[k] if k in D, else d.  d defaults to None. """
 70         pass
 71 
 72     def items(self): # real signature unknown; restored from __doc__#转换成列表
 73         """ D.items() -> a set-like object providing a view on D's items """
 74         pass
 75 
 76     def keys(self): # real signature unknown; restored from __doc__#得到字典里所有的key
 77         """ D.keys() -> a set-like object providing a view on D's keys """
 78         pass
 79 
 80     def pop(self, k, d=None): # real signature unknown; restored from __doc__#删除
 81         """
 82         D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
 83         If key is not found, d is returned if given, otherwise KeyError is raised
 84         """
 85         pass
 86 
 87     def popitem(self): # real signature unknown; restored from __doc__
 88         """
 89         D.popitem() -> (k, v), remove and return some (key, value) pair as a
 90         2-tuple; but raise KeyError if D is empty.
 91         """
 92         pass
 93 
 94     def setdefault(self, k, d=None): # real signature unknown; restored from __doc__
 95         """ D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D """
 96         pass
 97 
 98     def update(self, E=None, **F): # known special case of dict.update#更新
 99         """
100         D.update([E, ]**F) -> None.  Update D from dict/iterable E and F.
101         If E is present and has a .keys() method, then does:  for k in E: D[k] = E[k]
102         If E is present and lacks a .keys() method, then does:  for k, v in E: D[k] = v
103         In either case, this is followed by: for k in F:  D[k] = F[k]
104         """
105         pass
106 
107     def values(self): # real signature unknown; restored from __doc__#取字典里所有values
108         """ D.values() -> an object providing a view on D's values """
109         pass
原文地址:https://www.cnblogs.com/qwerdf/p/6551659.html