列表(list)
赋值方法:
l = [11,45,67,34,89,23]
l = list()
列表的方法:
#!/usr/bin/env python 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
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')
元组的方法:
#!/usr/bin/env python
class tuple(object):
"""
tuple() -> empty tuple
tuple(iterable) -> tuple initialized from iterable's items If the argument is a tuple, the return value is the same object.
"""
def count(self, value): # real signature unknown; restored from __doc__
'''某个元素在元素中出现的次数'''
""" T.count(value) -> integer -- return number of occurrences of value """
return 0 def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__
'''查找给定值第一次出现的位置'''
"""
T.index(value, [start, [stop]]) -> integer -- return first index of value.
Raises ValueError if the value is not present.
"""
return 0 def __add__(self, *args, **kwargs): # real signature unknown
""" Return self+value. """
pass def __contains__(self, *args, **kwargs): # real signature unknown
""" Return key in self. """
pass def __eq__(self, *args, **kwargs): # real signature unknown
""" Return self==value. """
pass def __getattribute__(self, *args, **kwargs): # real signature unknown
""" Return getattr(self, name). """
pass def __getitem__(self, *args, **kwargs): # real signature unknown
""" Return self[key]. """
pass def __getnewargs__(self, *args, **kwargs): # real signature unknown
pass def __ge__(self, *args, **kwargs): # real signature unknown
""" Return self>=value. """
pass def __gt__(self, *args, **kwargs): # real signature unknown
""" Return self>value. """
pass def __hash__(self, *args, **kwargs): # real signature unknown
""" Return hash(self). """
pass def __init__(self, seq=()): # known special case of tuple.__init__
"""
tuple() -> empty tuple
tuple(iterable) -> tuple initialized from iterable's items If the argument is a tuple, the return value is the same object.
# (copied from class doc)
"""
pass def __iter__(self, *args, **kwargs): # real signature unknown
""" Implement iter(self). """
pass def __len__(self, *args, **kwargs): # real signature unknown
""" Return len(self). """
pass def __le__(self, *args, **kwargs): # real signature unknown
""" Return self<=value. """
pass def __lt__(self, *args, **kwargs): # real signature unknown
""" Return self<value. """
pass def __mul__(self, *args, **kwargs): # real signature unknown
""" Return self*value.n """
pass @staticmethod # known case of __new__
def __new__(*args, **kwargs): # real signature unknown
""" Create and return a new object. See help(type) for accurate signature. """
pass def __ne__(self, *args, **kwargs): # real signature unknown
""" Return self!=value. """
pass def __repr__(self, *args, **kwargs): # real signature unknown
""" Return repr(self). """
pass def __rmul__(self, *args, **kwargs): # real signature unknown
""" Return self*value. """
pass
tuple
方法示例:
####
count()
>>> tup = ('a','b','c','b')
>>> tup.count('b')
2
####
index()
>>> tup = ('a','b','c','b')
>>> tup.index('b')
1
###
字典:
字典(dict):字典为一对键(key)和值(value)的对应关系,中间使用“:”分隔开。
key在字典中是唯一的,字典是无序的。
赋值字典:
dic = {'k1':'v1','k2':'v2','k3':'v3'}
字典的方法:
#!/usr/bin/env python
class dict(object):
"""
dict() -> new empty dictionary
dict(mapping) -> new dictionary initialized from a mapping object's
(key, value) pairs
dict(iterable) -> new dictionary initialized as if via:
d = {}
for k, v in iterable:
d[k] = v
dict(**kwargs) -> new dictionary initialized with the name=value pairs
in the keyword argument list. For example: dict(one=1, two=2)
"""
def clear(self): # real signature unknown; restored from __doc__
'''清空字典'''
""" D.clear() -> None. Remove all items from D. """
pass def copy(self): # real signature unknown; restored from __doc__
'''拷贝字典,浅拷贝'''
""" D.copy() -> a shallow copy of D """
pass @staticmethod # known case
def fromkeys(*args, **kwargs): # real signature unknown
'''首先有一个列表,这个列表将作为一个字典的key,如果不给值则所有key的值为空,如果给值就将值设置为key的值'''
""" Returns a new dict with keys from iterable and values equal to value. """
pass def get(self, k, d=None): # real signature unknown; restored from __doc__
'''根据key取值,如果没有这个key,不返回值'''
""" D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None. """
pass def items(self): # real signature unknown; restored from __doc__
'''所有key和值组成列表的形式'''
""" D.items() -> a set-like object providing a view on D's items """
pass def keys(self): # real signature unknown; restored from __doc__
'''所有key组成列表的形式'''
""" D.keys() -> a set-like object providing a view on D's keys """
pass def pop(self, k, d=None): # real signature unknown; restored from __doc__
'''获取key的值,并从字典中删除'''
"""
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
"""
pass def popitem(self): # real signature unknown; restored from __doc__
'''获取键值对,并在字典中删除,随机的'''
"""
D.popitem() -> (k, v), remove and return some (key, value) pair as a
2-tuple; but raise KeyError if D is empty.
"""
pass def setdefault(self, k, d=None): # real signature unknown; restored from __doc__
'''如果key不存在,则创建,如果key存在则返回key的值,不会修改key的值'''
""" D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D """
pass def update(self, E=None, **F): # known special case of dict.update
'''更新'''
"""
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]
"""
pass def values(self): # real signature unknown; restored from __doc__
'''所有值的列表形式'''
""" D.values() -> an object providing a view on D's values """
pass def __contains__(self, *args, **kwargs): # real signature unknown
""" True if D has a key k, else False. """
pass def __delitem__(self, *args, **kwargs): # real signature unknown
""" Delete self[key]. """
pass def __eq__(self, *args, **kwargs): # real signature unknown
""" Return self==value. """
pass def __getattribute__(self, *args, **kwargs): # real signature unknown
""" Return getattr(self, name). """
pass def __getitem__(self, y): # real signature unknown; restored from __doc__
""" x.__getitem__(y) <==> x[y] """
pass def __ge__(self, *args, **kwargs): # real signature unknown
""" Return self>=value. """
pass def __gt__(self, *args, **kwargs): # real signature unknown
""" Return self>value. """
pass def __init__(self, seq=None, **kwargs): # known special case of dict.__init__
"""
dict() -> new empty dictionary
dict(mapping) -> new dictionary initialized from a mapping object's
(key, value) pairs
dict(iterable) -> new dictionary initialized as if via:
d = {}
for k, v in iterable:
d[k] = v
dict(**kwargs) -> new dictionary initialized with the name=value pairs
in the keyword argument list. For example: dict(one=1, two=2)
# (copied from class doc)
"""
pass def __iter__(self, *args, **kwargs): # real signature unknown
""" Implement iter(self). """
pass def __len__(self, *args, **kwargs): # real signature unknown
""" Return len(self). """
pass def __le__(self, *args, **kwargs): # real signature unknown
""" Return self<=value. """
pass def __lt__(self, *args, **kwargs): # real signature unknown
""" Return self<value. """
pass @staticmethod # known case of __new__
def __new__(*args, **kwargs): # real signature unknown
""" Create and return a new object. See help(type) for accurate signature. """
pass def __ne__(self, *args, **kwargs): # real signature unknown
""" Return self!=value. """
pass def __repr__(self, *args, **kwargs): # real signature unknown
""" Return repr(self). """
pass def __setitem__(self, *args, **kwargs): # real signature unknown
""" Set self[key] to value. """
pass def __sizeof__(self): # real signature unknown; restored from __doc__
""" D.__sizeof__() -> size of D in memory, in bytes """
pass __hash__ = None
dict
方法示例:
####
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'])
####