Python: Sort list with parallel list

前端 未结 4 1404
渐次进展
渐次进展 2021-02-19 09:42

I have a list that is filled with HTML elements. I also have a list filled with date/times, which is parallel to the HTML list.

How can I sort the HTML list based on the

4条回答
  •  终归单人心
    2021-02-19 10:08

    Enumerate will give you a list of (index,item) tuples for each item in the list. You can sort this using the index to read the sort key from the timestamps list. Then peel off the html elements:

    sorted_elems = [elem for i,elem in sorted(enumerate(html_elements), 
                                              key=lambda x:timestamps[x[0]])]
    

    What could be simpler?

提交回复
热议问题