【问题标题】:are there any other ways to sort null lists in python?有没有其他方法可以在 python 中对空列表进行排序?
【发布时间】:2020-05-24 12:01:04
【问题描述】:

还有其他方法可以在不使用 lambda 的情况下对列表进行排序吗?

我有我的代码

import csv

def menu_list(filep): #function menu list path file

    menulists = []

    with open(filep) as csv_file: #read csv file
        csv_reader = csv.reader(csv_file, delimiter=',')
        for row in csv_reader:
            mList = []
            for i in range (len(row)):
                mList.append(row[i])
            menulists.append(mList)

    menulists.sort(key=lambda x: x[1])
    return menulists

我只是想知道是否还有其他方法可以对空列表进行排序

【问题讨论】:

  • “空列表”是什么意思?如果您不喜欢使用 lambda 表达式,可以将其替换为 operator.itemgetter(1)

标签: python sorting null opencsv


【解决方案1】:

我不确定你在这个 csv_file 中究竟有什么,但我认为在这种情况下你应该使用pandas

类似这样的:

df = pd.read_csv(file_csv)
df = df.sort_values(by=['col_name'])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-09
    • 1970-01-01
    • 2013-03-08
    • 1970-01-01
    • 1970-01-01
    • 2012-12-07
    相关资源
    最近更新 更多