python list and dict public method

1.Python list public method

| append(...)    #在列表末尾添加字符串。

| L.append(object) -> None -- append object to end、

例子:

 

1 >>> li = ['a','b','c']
2 >>> li.append('d')
3 >>> li
4 ['a', 'b', 'c', 'd']
View Code

| clear(...)    #直接清空字符串
| L.clear() -> None -- remove all items from L
|
| copy(...)  #复制字符串  shallow copy  浅复制。
| L.copy() -> list -- a shallow copy of L
|

 

1 >>> li = ['a','b','c']
2 >>> li02 = li.copy()
3 >>> li02
4 ['a', 'b', 'c']
View Code

 

 

| count(...)    #计算列表中元素出现的个数
| L.count(value) -> integer -- return number of occurrences of value
|

 

1 >>> li = ['a','b','c','a']
2 >>> li.count('a')
3 2
View Code

 

 

| extend(...)   #把另外一个列表中的元素一次性添加到当前列表当中
| L.extend(iterable) -> None -- extend list by appending elements from the iterable
|

 

1 >>> li = ['a','b','c','a']
2 >>> li02 = [1,2,3]
3 >>> li.extend(li02)
4 >>> print(li)
5 ['a', 'b', 'c', 'a', 1, 2, 3]
View Code

| index(...)  #搜索列表当中的元素从左到右,可以设置起始位置和结束位置,如果该列表当中没有搜索的元素,那么报错:ValueError: 'a' is not in list
| L.index(value, [start, [stop]]) -> integer -- return first index of value.
| Raises ValueError if the value is not present.

1 >>> li = ['a','b','c','a']
2 >>> li.index('a')
3 0
4 >>> li.index('a',2)
5 3
6 >>> li.index('a',1,2)
7 Traceback (most recent call last):
8   File "<stdin>", line 1, in <module>
9 ValueError: 'a' is not in list
View Code

| insert(...)    #从指定位置添加元素,该方法必须要有两个参数。index(位置,元素)
| L.insert(index, object) -- insert object before index
|

 

1 >>> li = ['a','b','c','a']
2 >>> li.insert(2,'d')
3 >>> li
4 ['a', 'b', 'd', 'c', 'a']
View Code

 

 

| pop(...)   #删除最后一个元素
| L.pop([index]) -> item -- remove and return item at index (default last).
| Raises IndexError if list is empty or index is out of range.
|

 

1 >>> li = ['a','b','c','a']
2 >>> li.pop()
3 'a'
4 >>> li
5 ['a', 'b', 'c']
View Code
还可以把删除的值赋予某一个变量,这样就能得到删除的值。在某些情况下能够节约代码。
>>> li = ['a','b','c','a'] >>> str01 = li.pop() >>> print(str01) a

 

 

| remove(...)    #移除指定元素,在列表当中从左到右开始检索,遇到则删除第一个,后面的不删除。
| L.remove(value) -> None -- remove first occurrence of value.
| Raises ValueError if the value is not present.

1 >>> li = ['a','b','c','a']
2 >>> 
3 >>> li.remove('c')
4 >>> li
5 ['a', 'b', 'a']
6 >>> li = ['a','b','c','a']
7 >>> li.remove('a')
8 >>> li
9 ['b', 'c', 'a']
View Code

| reverse(...)  #对列表进行倒叙排列
| L.reverse() -- reverse *IN PLACE*
|
| sort(...)  #对列表进行排序
| L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE*
|
| ----------------------------------------------------------------------

 

python3 dictionary public method

 

| clear(...)   #清空字典
| D.clear() -> None. Remove all items from D.
|
| copy(...)  #复制字典
| D.copy() -> a shallow copy of D
|
| fromkeys(iterable, value=None, /) from builtins.type
| Returns a new dict with keys from iterable and values equal to value.

1 >>> t1 = ('a','b','c')
2 >>> dic = dic.fromkeys(t1)
3 >>> print(dic)
4 {'a': None, 'b': None, 'c': None}
5 >>> dic = dic.fromkeys(t1,'like')
6 >>> print(dic)
7 {'a': 'like', 'b': 'like', 'c': 'like'}
View Code

| get(...)  #返回字典中key对应的值,如果值不存在则返回None
| D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.

1 >>> dic = {'k1':1,'k2':2}
2 >>> dic.get('k1')
3 1
4 >>> dic.get('k3')
View Code

 

| items(...)  以列表的形式返回字典中可遍历的(键, 值) 元组数组
| D.items() -> a set-like object providing a view on D's items

1 >>> dic.items()
2 dict_items([('k1', 1), ('k2', 2)])
View Code

 

1 >>> dic = {'k1':1,'k2':2}
2 >>> for k,v in dic.items():
3 ...     print(k,v)
4 ... 
5 k1 1
6 k2 2
View Code

 

| keys(...)   #以列表的形式返回字典中的键(keys)
| D.keys() -> a set-like object providing a view on D's keys

1 >>> dic = {'k1':1,'k2':2}
2 >>> dic.keys()
3 dict_keys(['k1', 'k2'])
View Code

 

| pop(...)  #删除字典中指定的keys以及value,如果删除的keys在字典中不存在则返回KeyError
| D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
| If key is not found, d is returned if given, otherwise KeyError is raised

1 >>> dic = {'k1':1,'k2':2}
2 >>> dic.pop('k1')
3 1
4 >>> dic
5 {'k2': 2}
View Code

| popitem(...)   #删除字典中最后一个keys以及对应的value,如果一直删除直到字典为空时,则报错KeyError: 'popitem(): dictionary is empty'
| D.popitem() -> (k, v), remove and return some (key, value) pair as a
| 2-tuple; but raise KeyError if D is empty.

 1 >>> dic = {'k1':1,'k2':2}
 2 >>> dic.popitem()
 3 ('k2', 2)
 4 >>> dic
 5 {'k1': 1}
 6 >>> dic.popitem()
 7 ('k1', 1)
 8 >>> dic
 9 {}
10 >>> dic.popitem()
11 Traceback (most recent call last):
12   File "<stdin>", line 1, in <module>
13 KeyError: 'popitem(): dictionary is empty'
View Code

 


| setdefault(...)  #和get()方法类似, 如果键已经不存在于字典中,将会添加键并将值设为默认
| D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D
|

>>> dic = {'k1':1,'k2':2}
>>> print(dic.setdefault('k1',None))
1
>>> print(dic.setdefault('k3',None))
None
>>> dic
{'k1': 1, 'k2': 2, 'k3': None}
>>> print(dic.setdefault('k4',4))
4
>>> dic
{'k1': 1, 'k2': 2, 'k3': None, 'k4': 4}

| update(...)  #把字典2的key,value 更新到字典1.合并成为一个字典
| D.update([E, ]**F) -> None. Update D from dict/iterable E and F.
| If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]
| If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v
| In either case, this is followed by: for k in F: D[k] = F[k]

1 >>> dic = {'k1':1,'k2':2}
2 >>> dic02 = {'a1':'a','b1':'b'}
3 >>> dic.update(dic02)
4 >>> dic
5 {'k1': 1, 'k2': 2, 'a1': 'a', 'b1': 'b'}
View Code、


| values(...)    #获取字典中的values,并且以列表的形式获取。
| D.values() -> an object providing a view on D's values

 

 

 1 >>> dic
 2 {'k1': 1, 'k2': 2, 'a1': 'a', 'b1': 'b'}
 3 >>> for i in dic.values():
 4 ...     print(i)
 5 ... 
 6 1
 7 2
 8 a
 9 b
10 >>> dic.values()
11 dict_values([1, 2, 'a', 'b'])
View Code

 

posted @ 2017-04-10 15:32  Nice_keep-going  阅读(219)  评论(0)    收藏  举报