We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
(Dictionary) keys() Python 字典(Dictionary) keys() 函数以列表返回一个字典所有的键。 keys()方法语法:
dict.keys()
返回值:
返回一个字典所有的键。
例如:
#!/usr/bin/python dict = {'Name': 'Zara', 'Age': 7} print "Value : %s" % dict.keys() #Value : ['Age', 'Name']
The text was updated successfully, but these errors were encountered:
要说Counter()就不得不说一下collections
collections是Python内建的一个集合模块,提供了许多有用的集合类。 如 OrderedDict() deque() defaultdict() namedtuple() Counter()
其中 Counter() 是一个简单的计数器,例如,统计字符出现的个数:
>>> from collections import Counter >>> c = Counter() >>> for ch in 'programming': ... c[ch] = c[ch] + 1 ... >>> c Counter({'g': 2, 'm': 2, 'r': 2, 'a': 1, 'i': 1, 'o': 1, 'n': 1, 'p': 1})
OrderedDict() 使用dict时,Key是无序的。在对dict做迭代时,我们无法确定Key的顺序。如果要保持Key的顺序,可以用OrderedDict:
from collections import OrderedDict d = dict([('a', 1), ('b', 2), ('c', 3)]) d # dict的Key是无序的 {'a': 1, 'c': 3, 'b': 2} od = OrderedDict([('a', 1), ('b', 2), ('c', 3)] od # OrderedDict的Key是有序的 OrderedDict([('a', 1), ('b', 2), ('c', 3)])
OrderedDict的Key会按照插入的顺序排列,不是Key本身排序 OrderedDict可以实现一个FIFO(先进先出)的dict,当容量超出限制时,先删除最早添加的Key
deque() deque拥有list的append()和pop()方法之外,还支持appendleft()和popleft(),这样就可以非常高效地往头部添加或删除元素 适合用于队列和栈
from collections import deque q = deque(['a', 'b', 'c']) q.append('x') q.appendleft('y') q #deque(['y', 'a', 'b', 'c', 'x'])
Sorry, something went wrong.
No branches or pull requests
(Dictionary) keys()
Python 字典(Dictionary) keys() 函数以列表返回一个字典所有的键。
keys()方法语法:
返回值:
例如:
The text was updated successfully, but these errors were encountered: