简体   繁体   English

如何使用特定列按升序对列表进行排序,而不使用 function 中的构建? (Python)

[英]How do I sort a list in ascending order using a specific column, without using a build in function? (Python)

I have imported a CSV file to a list in python containing animal data with the following columns:我已将 CSV 文件导入到 python 中的列表中,其中包含具有以下列的动物数据:

Sanctuary Identification,Type,Breed,Vaccinated,Neutered

P23533,Cat,Persian,Yes,Yes

P43533,Dog,Poodle,Yes,No

P18754,Dog...

The data appears in the following format:数据以下列格式显示:

[[P23533,Cat,Persian,Yes,Yes],[P43533,Dog,Poodle,Yes,No],[P18754,Dog...]]

I would like to sort the list in ascending order of their Sanctuary Indentifaction, ie:我想按他们的 Sanctuary Indentifaction 升序对列表进行排序,即:

P1111, Cat, ..
P2222 Dog, ..
P3333.. etc

I cannot use any build in libraries, can anyone suggest how I might go about this?我不能在库中使用任何构建,任何人都可以建议我如何 go 关于这个?

You could sort the nested list using sorted built-in function, then pass in the first column as the key to be sorted.您可以使用sorted内置 function 对嵌套列表进行排序,然后传入第一列作为要排序的键。 Below is the example.下面是示例。

In [1]: my_list = [["P23533","Cat","Persian","Yes","Yes"],["P43533","Dog","Poodle","Yes","No"], ["P1111","Asd","Test","
   ...: Yes","Yes"]]

In [2]: sorted(my_list, key=lambda x: x[0])
Out[2]:
[['P1111', 'Asd', 'Test', 'Yes', 'Yes'],
 ['P23533', 'Cat', 'Persian', 'Yes', 'Yes'],
 ['P43533', 'Dog', 'Poodle', 'Yes', 'No']]

Alternatively, you can use pandas.read_csv to load your csv into DataFrame object, then use sort_values function: Alternatively, you can use pandas.read_csv to load your csv into DataFrame object, then use sort_values function:

In [10]: df.sort_values(["SanctuaryID"]).reset_index(drop=True)
Out[10]:
  SanctuaryID Animal     Type Col4 Col5
0       P1111    Asd     Test  Yes  Yes
1      P23533    Cat  Persian  Yes  Yes
2      P43533    Dog   Poodle  Yes   No

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 python 程序以升序对列表进行排序,而不使用排序 function - python program to sort a list in ascending order without using the sort function 如何在不使用排序 function 的情况下查找列表中的元素是升序还是降序? - How to find if the elements in a list are in ascending order or in descending order without using sort function? 自定义使用 python 中的排序函数按升序对列表进行排序 - Custom Sort the list in ascending order with using sorted functions in python 如何使用 Python 按升序对 pandas dataframe 进行排序 - How to sort pandas dataframe in ascending order using Python Python:按升序使用键对字典进行排序 - Python : Sort a dictionary by using keys in ascending order 如何按升序对python进行排序? - How can I sort in python in ascending order? 如何在不使用列表的情况下在python中按升序或降序排列5个用户的号码? - How to arrange 5 user's number in ascending or descending order in python without using list? 如何使列表列表中的每个列表按升序排序? - How do I make each list in the list of lists sort in ascending order? 如何在 Python 中按升序对嵌套列表中的项目进行排序? - How to sort items in nested list in ascending order in Python? Python CSV 读数。 使用升序和降序按 2 列排序 - Python CSV reading. Sort by 2 columns using ascending and descending order
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM