Python 排序计数器
Python Sort Counter
我无法正确反击 sort/displayed
我的密码是
with open('nonweb') as f:
for i in f:
entry.append(i.strip())
counter=Counter(entry)
print counter
for z in counter:
print '%s : %d' % (z, counter[z])
计数器是
Counter({'192.168.1.45 UDP 137': 2262, '192.168.1.85 UDP 137': 2262, '192.119.43.56 UDP 53': 78, '192.119.39.68 UDP 53': 78, '192.168.92.223 UDP 10111': 78, '192.168.1.13 UDP 137': 72, '192.167.80.106 TCP 8087': 48, '192.168.1.127 UDP 8083': 48, '192.168.1.129 UDP 8083': 44, '192.218.30.124 UDP 53': 32, '192.77.58.44 TCP 5282': 24, '192.168.1.13 UDP 138': 18, '192.168.1.69 UDP 138': 14, '192.168.1.85 UDP 138': 10, '192.168.1.57 UDP 138': 10, '192.168.1.33 UDP 138': 10, '192.168.1.45 UDP 138': 10, '192.168.1.92 UDP 138': 10, '192.168.1.97 UDP 138': 10, '192.168.1.79 UDP 138': 10, '192.168.1.60 UDP 138': 10, '192.168.1.32 UDP 138': 10, '192.168.1.18 UDP 138': 10, '192.168.1.58 UDP 138': 10, '192.168.1.95 UDP 138': 10, '192.168.1.19 UDP 138': 10, '192.168.1.143 UDP 138': 10, '192.168.1.138 UDP 138': 10, '192.168.1.99 UDP 138': 10, '192.168.1.139 UDP 138': 10, '192.168.1.96 UDP 138': 10, '192.168.1.140 UDP 138': 10, '192.168.1.137 UDP 138': 10, '192.168.1.59 UDP 138': 10, '192.171.70.154 UDP 53': 6, '216.163.251.236 TCP 42590': 2, '192.168.1.140 TCP 56230': 2})
但是当我尝试以可呈现的格式显示它时,它的打印顺序与计数器列表不同。 (最好没有semi:colon)
192.168.1.45 UDP 137 : 2262
192.168.1.85 UDP 137 : 2262
192.168.1.85 UDP 138 : 10
192.168.1.57 UDP 138 : 10
192.168.1.33 UDP 138 : 10
192.168.1.45 UDP 138 : 10
192.168.1.92 UDP 138 : 10
192.168.1.129 UDP 8083 : 44
192.168.1.97 UDP 138 : 10
192.168.1.13 UDP 137 : 72
192.168.1.79 UDP 138 : 10
由于 Counter
是作为字典实现的,所以它并没有真正的秩序感。如果您想按顺序手动迭代其元素,您需要创建这样的顺序:
# reverse=True to get descending order
for k, v in sorted(counter_obj.items(), key=lambda x: x[1], reverse=True):
print(k, v)
或者按照@tobias_k在评论中的建议,简单地遍历most_common
方法返回的元组列表:
for k, v in c.most_common():
print(k, v)
有趣的是 most_common
的实现方式几乎完全相同:
def most_common(self, n=None):
'''List the n most common elements and their counts from the most
common to the least. If n is None, then list all element counts.
>>> Counter('abcdeabcdabcaba').most_common(3)
[('a', 5), ('b', 4), ('c', 3)]
'''
# Emulate Bag.sortedByCount from Smalltalk
if n is None:
return sorted(self.items(), key=_itemgetter(1), reverse=True)
return _heapq.nlargest(n, self.items(), key=_itemgetter(1))
您的 Counter 对象中有一个 dict。
顺序不是随机的而是:
Keys and values are iterated over in an arbitrary order which is non-random, varies across Python implementations, and depends on the dictionary’s history of insertions and deletions.
您可以使用 orderddict.
来解决这个问题
Ordered dictionaries are just like regular dictionaries but they remember the order that items were inserted.
使用counter.most_common()
:
for k,v in c.most_common():
print(k,v)
我无法正确反击 sort/displayed
我的密码是
with open('nonweb') as f:
for i in f:
entry.append(i.strip())
counter=Counter(entry)
print counter
for z in counter:
print '%s : %d' % (z, counter[z])
计数器是
Counter({'192.168.1.45 UDP 137': 2262, '192.168.1.85 UDP 137': 2262, '192.119.43.56 UDP 53': 78, '192.119.39.68 UDP 53': 78, '192.168.92.223 UDP 10111': 78, '192.168.1.13 UDP 137': 72, '192.167.80.106 TCP 8087': 48, '192.168.1.127 UDP 8083': 48, '192.168.1.129 UDP 8083': 44, '192.218.30.124 UDP 53': 32, '192.77.58.44 TCP 5282': 24, '192.168.1.13 UDP 138': 18, '192.168.1.69 UDP 138': 14, '192.168.1.85 UDP 138': 10, '192.168.1.57 UDP 138': 10, '192.168.1.33 UDP 138': 10, '192.168.1.45 UDP 138': 10, '192.168.1.92 UDP 138': 10, '192.168.1.97 UDP 138': 10, '192.168.1.79 UDP 138': 10, '192.168.1.60 UDP 138': 10, '192.168.1.32 UDP 138': 10, '192.168.1.18 UDP 138': 10, '192.168.1.58 UDP 138': 10, '192.168.1.95 UDP 138': 10, '192.168.1.19 UDP 138': 10, '192.168.1.143 UDP 138': 10, '192.168.1.138 UDP 138': 10, '192.168.1.99 UDP 138': 10, '192.168.1.139 UDP 138': 10, '192.168.1.96 UDP 138': 10, '192.168.1.140 UDP 138': 10, '192.168.1.137 UDP 138': 10, '192.168.1.59 UDP 138': 10, '192.171.70.154 UDP 53': 6, '216.163.251.236 TCP 42590': 2, '192.168.1.140 TCP 56230': 2})
但是当我尝试以可呈现的格式显示它时,它的打印顺序与计数器列表不同。 (最好没有semi:colon)
192.168.1.45 UDP 137 : 2262
192.168.1.85 UDP 137 : 2262
192.168.1.85 UDP 138 : 10
192.168.1.57 UDP 138 : 10
192.168.1.33 UDP 138 : 10
192.168.1.45 UDP 138 : 10
192.168.1.92 UDP 138 : 10
192.168.1.129 UDP 8083 : 44
192.168.1.97 UDP 138 : 10
192.168.1.13 UDP 137 : 72
192.168.1.79 UDP 138 : 10
由于 Counter
是作为字典实现的,所以它并没有真正的秩序感。如果您想按顺序手动迭代其元素,您需要创建这样的顺序:
# reverse=True to get descending order
for k, v in sorted(counter_obj.items(), key=lambda x: x[1], reverse=True):
print(k, v)
或者按照@tobias_k在评论中的建议,简单地遍历most_common
方法返回的元组列表:
for k, v in c.most_common():
print(k, v)
有趣的是 most_common
的实现方式几乎完全相同:
def most_common(self, n=None):
'''List the n most common elements and their counts from the most
common to the least. If n is None, then list all element counts.
>>> Counter('abcdeabcdabcaba').most_common(3)
[('a', 5), ('b', 4), ('c', 3)]
'''
# Emulate Bag.sortedByCount from Smalltalk
if n is None:
return sorted(self.items(), key=_itemgetter(1), reverse=True)
return _heapq.nlargest(n, self.items(), key=_itemgetter(1))
您的 Counter 对象中有一个 dict。 顺序不是随机的而是:
Keys and values are iterated over in an arbitrary order which is non-random, varies across Python implementations, and depends on the dictionary’s history of insertions and deletions.
您可以使用 orderddict.
来解决这个问题Ordered dictionaries are just like regular dictionaries but they remember the order that items were inserted.
使用counter.most_common()
:
for k,v in c.most_common():
print(k,v)