import random


def make_ip():
    ip_num = [int(random.random() * 20) for i in range(40000)]
    for i in range(0, len(ip_num), 4):
        with open('ip', 'a', encoding='utf8') as f:
            f.write('%d.%d.%d.%d\n' % tuple(ip_num[i:i+4]))


def sort_ip():
    ip_map = {}
    with open('ip', 'r', encoding='utf8') as f:
        for ip in f:
            ip = ip.strip()
            if not ip or ip.startswith('#'):
                continue

            ip_map[ip] = ip_map.setdefault(ip, 0) + 1

        print(len(ip_map))
        print(ip_map)
        print(sorted(ip_map.items(), key=lambda item:item[1], reverse=True))

if "__main__" == __name__:
    # make_ip()
    sort_ip()
