Python 3.x Python对具有不同属性的标记进行排序
我有一个来自Python 3.x Python对具有不同属性的标记进行排序,python-3.x,sorting,attributes,tags,Python 3.x,Sorting,Attributes,Tags,我有一个来自links=set(soup.findAll('a'))的列表,现在我想对它们进行排序,但是links=sorted(links)出现错误,我该怎么排序 原始列表和任何其他属性: <a href="//weathernews.jp/typhoon/">台風</a> <a class="list" href="https://weathernews.jp/s/topics/201809/190035/?fm=tp_index"></a>
links=set(soup.findAll('a'))
的列表,现在我想对它们进行排序,但是links=sorted(links)
出现错误,我该怎么排序
原始列表和任何其他属性:
<a href="//weathernews.jp/typhoon/">台風</a>
<a class="list" href="https://weathernews.jp/s/topics/201809/190035/?fm=tp_index"></a>
<a class="list" href="https://weathernews.jp/s/topics/201809/130155/?fm=tp_index"></a>
<a href="//weathernews.jp/warning/">警報・注意報</a>
<a class="list" href="https://weathernews.jp/s/topics/201809/140125/?fm=tp_index"></a>
<a class="list" href="https://weathernews.jp/s/topics/201809/170145/?fm=tp_index"></a>
我想把它分类
<a class="list" href="https://weathernews.jp/s/topics/201809/190035/?fm=tp_index"></a>
<a class="list" href="https://weathernews.jp/s/topics/201809/170145/?fm=tp_index"></a>
<a class="list" href="https://weathernews.jp/s/topics/201809/140125/?fm=tp_index"></a>
<a class="list" href="https://weathernews.jp/s/topics/201809/130155/?fm=tp_index"></a>
<a href="//weathernews.jp/typhoon/">台風</a>
<a href="//weathernews.jp/warning/">警報・注意報</a>
我用这个
links = sorted(links, key=lambda x: str(x), reverse=False)
我建议您首先将链接映射到字符串,然后对其进行排序
links = set(soup.findAll('a'))
links = map(str, links)
links = sorted(links)
或
简单地说
links = sorted(set(map(str, links)))