python - 如何按降序排列我的链接(我有链接的值,(num_to_words(v)))

标签 python algorithm sorting pycharm

我正在制作网络爬虫,现在我需要排序算法,它可以按降序对我的链接进行排序,以查看哪个链接出现在该网页中的次数最多。这是我用 python 编写的代码:

import requests
from bs4 import BeautifulSoup
from collections import defaultdict

all_links = defaultdict(int)

def webpages():

        url = 'http://www.hm.com/lv/department/MEN'
        source_code = requests.get(url)
        text = source_code.text
        soup = BeautifulSoup(text)
        for link in soup.findAll ('a', {'class':' ', 'rel':'nofollow'}):
            href = link.get('href')
            print(href)
            get_single_item_data(href)
        return all_links

def get_single_item_data(item_url):
    source_code = requests.get(item_url)
    text = source_code.text
    soup = BeautifulSoup(text)
    for link in soup.findAll('a'):
        href = link.get('href')
        if href and href.startswith('http://www.'):
            if href:
                all_links[href] += 1
            print(href)


webpages()

units = ["", "one", "two", "three", "four", "five",
         "six", "seven", "eight", "nine "]
teens = ["", "eleven", "twelve", "thirteen", "fourteen",
         "fifteen", 'sixteen', "seventeen", "eighteen", "nineteen"]
tens = ["", "ten", "twenty", "thirty", "forty",
        "fifty", "sixty", "seventy", "eighty", "ninety"]
thousands = ["", "thousand", "million", "billion", "trillion",
             "quadrillion", "quintillion", "sextillion", "septillion", "octillion",
             "nonillion", "decillion", "undecillion", "duodecillion", "tredecillion",
             "quattuordecillion", "sexdecillion", "septendecillion", "octodecillion",
             "novemdecillion", "vigintillion "]


def num_to_words(n):
    words = []
    if n == 0:
        words.append("zero")
    else:
        num_str = "{}".format(n)
        groups = (len(num_str) + 2) // 3
        num_str = num_str.zfill(groups * 3)
        for i in range(0, groups * 3, 3):
            h = int(num_str[i])
            t = int(num_str[i + 1])
            u = int(num_str[i + 2])
            print()
            print(units[i])
            g = groups - (i // 3 + 1)
            if h >= 1:
                words.append(units[h])
                words.append("hundred")
                if int(num_str) % 100:  # if number  modulo 100 has remainder  add "and" i.e one hundred and ten
                    words.append("and")
            if t > 1:
                words.append(tens[t])
                if u >= 1:
                    words.append(units[u])
            elif t == 1:
                if u >= 1:
                    words.append(teens[u])
                else:
                    words.append(tens[t])
            else:
                if u >= 1:
                    words.append(units[u])

            if g >= 1 and (h + t + u) > 0:
                words.append(thousands[g])
    return " ".join(words)

for k, v in webpages().items():
    print(k, num_to_words(v))

最佳答案

如果它们存储在数组中,您可以对数组进行排序。
例如:

# Array 
a = [6, 2, 9, 3]
# sort the array 
a.sort() 

也许此链接也有帮助:Link about sorting

关于python - 如何按降序排列我的链接(我有链接的值,(num_to_words(v))),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28517047/

相关文章:

c++ - 插入排序 - 处理空列表

c++ - 通过将某人排序到链表中来添加某人时出现段错误的原因

python - 居中窗口python tkinter

java - 非单调插值方法

python - 更好的编程方式

algorithm - BASH大括号扩展算法

java - 如何使用 Google Guava 字典按对象字段迭代对象列表?

python - 在Python中将浮点列表加入空格分隔的字符串

python - 使用 python 的 lxml 剥离内联标签

python - 规范化遗漏波兰语字符