>>>
help(dict)
Help on dict object:
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)
|
| Methods defined here:
|
| __contains__(self, key, /)
| True if the dictionary has the specified key, else False.
|
| __delitem__(self, key, /)
| Delete self[key].
|
| __eq__(self, value, /)
| Return self==value.
|
| __ge__(self, value, /)
| Return self>=value.
|
| __getattribute__(self, name, /)
| Return getattr(self, name).
|
|
__getitem__
(...)
| x.__getitem__(y) <==> x[y]
|
| __gt__(self, value, /)
| Return self>value.
|
| __init__(self, /, *args, **kwargs)
| Initialize self. See help(type(self)) for accurate signature.
|
|
__iter__
(self, /)
| Implement iter(self).
|
| __le__(self, value, /)
| Return self<=value.
|
| __len__(self, /)
| Return len(self).
|
| __lt__(self, value, /)
| Return self
|
| __ne__(self, value, /)
| Return self!=value.
|
| __repr__(self, /)
| Return repr(self).
|
| __setitem__(self, key, value, /)
| Set self[key] to value.
|
| __sizeof__(...)
| D.__sizeof__() -> size of D in memory, in bytes
|
| clear(...)
| D.clear() -> None. Remove all items from D.
|
| copy(...)
| D.copy() -> a shallow copy of D
|
| get(self, key, default=None, /)
| Return the value for key if key is in the dictionary, else default.
|
| items(...)
| D.items() -> a set-like object providing a view on D's items
|
| keys(...)
| D.keys() -> a set-like object providing a view on D's keys
|
| pop(...)
| 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
|
| popitem(...)
| D.popitem() -> (k, v), remove and return some (key, value) pair as a
| 2-tuple; but raise KeyError if D is empty.
|
| setdefault(self, key, default=None, /)
| Insert key with a value of default if key is not in the dictionary.
|
| Return the value for key if key is in the dictionary, else default.
|
| 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]
|
| values(...)
| D.values() -> an object providing a view on D's values
|
| ----------------------------------------------------------------------
| Class methods defined here:
|
| fromkeys(iterable, value=None, /) from builtins.type
| Create a new dictionary with keys from iterable and values set to value.
|
| ----------------------------------------------------------------------
| Static methods defined here:
|
| __new__(*args, **kwargs) from builtins.type
| Create and return a new object. See help(type) for accurate signature.
|
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| __hash__ = None
>>>
help(sorted)
Help on built-in function sorted in module builtins:
sorted(
iterable
, /, *, key=None, reverse=False)
Return a new list containing all items from the iterable in ascending order.
A custom key function can be supplied to customize the sort order, and the
reverse flag can be set to request the result in descending order.
>>>
PS: 任何具有__iter__()或__getitem__()方法的对象,Python就认为它是一个iterable。
iterable
可迭代对象可迭代的
>>>
help(operator.itemgetter(1))
Help on itemgetter object:
class itemgetter(builtins.object)
|
itemgetter
(item, ...) --> itemgetter object
|
| Return a callable object that fetches the given item(s) from its operand.
| After f = itemgetter(2), the call f(r) returns r[2].
| After g = itemgetter(2, 5, 3), the call g(r) returns (r[2], r[5], r[3])
|
| Methods defined here:
|
| __call__(self, /, *args, **kwargs)
| Call self as a function.
|
| __getattribute__(self, name, /)
| Return getattr(self, name).
|
| __reduce__(...)
| Return state information for pickling
|
| __repr__(self, /)
| Return repr(self).
|
| ----------------------------------------------------------------------
| Static methods defined here:
|
| __new__(*args, **kwargs) from builtins.type
| Create and return a new object. See help(type) for accurate signature.
>>>
示例
>>> d
#字典类型
{'A': 2, 'B': 1, 'C': 5, 'D': 1}
>>>
d.items()
dict_items([('A', 2), ('B', 1), ('C', 5), ('D', 1)])
>>>
d.keys()
dict_keys(['A', 'B', 'C', 'D'])
>>>
import operator
>>> sorted(d.items(), key=
operator.itemgetter(0)
, reverse=True)
#按照key排序
[('D', 1), ('C', 5), ('B', 1), ('A', 2)]
>>> sorted(d.items(), key=
operator.itemgetter(1)
, reverse=True)
#按照value排序
[('C', 5), ('A', 2), ('B', 1), ('D', 1)]
>>> d4=sorted(d.items(), key=operator.itemgetter(
1
), reverse=True)
#按照value的大小对字典d4进行降序排序
>>> d4
[('C', 5), ('A', 2), ('B', 1), ('D', 1)]
#排序后的结果,它是一个list,元素为tuple
>>> d4[0]
('C', 5)
>>> d4[0][0]
'C'
>>> d4[0][1]
5
>>>