对嵌套列表进行排序:从排序中排除第一项 [英] Sort nested list: Exclude first item from sorting
问题描述
我有一个csv file
,其中包括当前的茶点余额(以逗号分隔,但是在此示例中,删除了逗号,
以提高可读性):
I have a csv file
which includes the current balance of my refreshments (it is comma separated, but in this example commas ,
are removed for improved readability):
NAME AMOUNT PRICE
Coca-Cola 8 1.25
Fanta 6 1.29
Dr. Pepper 2 2.20
Sprite 10 1.35
Guarana 6 1.80
Pepsi 4 1.25
我将所有数据读入内存(列表),并使用以下命令进行所有必要的
I read all the data into the memory (list) and do all the necessary editing using:
import csv
# format list for refreshments
items_list = []
with open("my_refresments.csv", newline='') as fileOpener:
open_csv = csv.reader(fileOpener)
for rows in open_csv:
items_list.append(rows)
列表"items_list"现在显示为:
The list 'items_list' now prints out as:
items_list = [["NAME","AMOUNT","PRICE"],
["Coca-Cola","8","1.25"],
["Fanta","6","1.29"],
["Dr. Pepper","2","2.20"],
["Sprite","10","1.35"],
["Guarana","6","1.80"],
["Pepsi","4","1.25"]]
问题
什么给了我这种排序:
QUESTION
What gives me this kind of sorting:
# Notice that this is sorted, but items_list[0] is at it's place
items_list = [["NAME","AMOUNT","PRICE"],
["Coca-Cola","8","1.25"],
["Fanta","6","1.29"],
["Dr. Pepper","2","2.20"],
["Sprite","10","1.35"],
["Guarana","6","1.80"],
["pepsi","4","1.25"]]
除了第一行items_list[0]
之外的所有内容都应保持原样,不会被人为误解.
Everything except first row items_list[0]
should stay unsorted ergo untouched where it stands.
通常a.sort()
是嵌套列表的一个不错的选择,因为它不会对列表中的所有单个项目进行排序,而是对彼此比较的列表进行排序.在这种情况下,我不能使用items_list.sort()
,因为它还将在csv文件中包含标头,我想将其保留在它已经位于的位置:items_list[0]
.这是使用items_list.sort()
时发生的情况:
Generally a.sort()
is a good option with nested lists because it will not sort all the individual items inside the lists, but instead it sorts the lists compared one to another. In this case I can't use items_list.sort()
, because it will include also the headers in csv file, which I want to stay just where it is already located: items_list[0]
. Here is what happens when using items_list.sort()
:
items_list.sort()
[['Coca-Cola', '8', '1.25'],
['Fanta', '6', '1.29'],
['Dr. Pepper', '2', '2.20'],
['Sprite', '10', '1.35'],
['Guarana', '6', '1.80'],
['NAME', 'AMOUNT', 'PRICE'],
['Pepsi', '4', '1.25']]
奇怪的是,如果我用lower case
编写所有的茶点,那会起作用,因为TEXT > text
,但是我不想这样做.我想使用slice
排除第一行(标题),但是它似乎并没有任何效果(或者也许我做错了):
Strangely, if I write all the refreshments with lower case
, it will work, because TEXT > text
, but I don't want to do that. I would like to use slice
to exclude the first row (headers), but it doesn't seem to have any effect what so ever (or maybe I'm doing it wrong):
# This doesn't do anything
items_list[1:].sort()
一种可能的解决方案
我能做的是
One possible solution
What I could do is to
- 首先以一种或另一种方式将标头复制到另一个列表中
temp_list = [items_list[0]]
- 使用
del items_list[0]
语句删除items_list[0]
- 使用
items_list.sort()
对列表进行排序,最后 - 使用
items_list.insert(0, temp_list)
将标头插入已排序列表的
index(0)
- first copy headers into another list in one way or another
temp_list = [items_list[0]]
- delete
items_list[0]
using thedel items_list[0]
statement - sort the list using
items_list.sort()
, and finally - insert headers into the sorted list's
index(0)
withitems_list.insert(0, temp_list)
像这样:
def sort_csv_list_exclude_headers(file_name):
items_list = []
with open(file_name, newline='') as fileOpener:
open_csv = csv.reader(fileOpener)
for rows in open_csv:
items_list.append(rows)
temp_list = [items_list[0]]
del items_list[0]
items_list.sort()
items_list.insert(0, temp_list[0])
with open(file_name, "w") as fileWriter:
write_csv = csv.writer(fileWriter, lineterminator='\n')
write_csv.writerows(items_list)
sort_csv_list_exclude_headers("SODA_BALANCE.csv")
实际上,当将csv与超过1000000行的刷新"一起使用时,这通常是非常好的和简单的.
Actually this is pretty good and simple in general when using csv with > 1 000 000 rows of "refreshments".
还有其他(更简单的)方法吗?
Is there any other (more simple) method?
推荐答案
一个干净的衬里将是:
items_list[1:] = sorted(items_list[1:])
引用: https://stackoverflow.com/a/5827649/937153
这篇关于对嵌套列表进行排序:从排序中排除第一项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!