Python 使用OrderedDict以与CSV文件相反的顺序输出列表

Python 使用OrderedDict以与CSV文件相反的顺序输出列表,python,collections,Python,Collections,我需要导入包含以下内容的CSV文件: name,mean performance,std dev Alice,100,0 Bob,100,5 Clare,100,10 Dennis,90,0 Eva,90,5 并将输出排序为: {'Dennis': (90.0, 0.0), 'Clare': (100.0, 10.0), 'Eva': (90, 5.0), 'Bob': (100.0, 5.0), 'Alice': (100.0, 0.0)} 到目前为止,我已经: import csv i

我需要导入包含以下内容的CSV文件:

name,mean performance,std dev
Alice,100,0
Bob,100,5 
Clare,100,10
Dennis,90,0
Eva,90,5
并将输出排序为:

{'Dennis': (90.0, 0.0), 'Clare': (100.0, 10.0), 'Eva': (90, 5.0), 'Bob': (100.0, 5.0), 'Alice': (100.0, 0.0)}
到目前为止,我已经:

import csv
import collections

def sailorPerf(filename, header=True):

    with open(filename, mode='r') as csvfile:
        r = csv.reader(csvfile)
        if header==True:
            next(r)
        od = collections.OrderedDict((row[0], row[1]) for row in r)
    print (od)
哪些产出:

OrderedDict([('Alice', ' 100'), ('Bob', ' 100'), ('Clare', ' 100'), ('Dennis', ' 90'), ('Eva', ' 90')])

我想知道如何将第三列添加到结果中,以及如何更改格式以从输出中删除ordereddict部分,以及如何更改输出的名称和输出,使其与预期结果中的名称和输出一致

(行[0],行[1])
更改为
(行[0],元组(行[1:])
,将第二行作为包含索引1中所有元素的列表-列表末尾,然后将其转换为元组。

(行[0],行[1])
更改为
(行[0],元组(行[1:])
将第二列作为包含索引1中所有元素的列表,然后将其转换为元组。

您需要按转换为int的第二列和第三列对数据进行排序:

from collections import OrderedDict
def sailorPerf(filename, header=True):
    with open(filename, mode='r') as csvfile:
        r = csv.reader(csvfile)
        if header:
            next(r)
        od = OrderedDict((name, tuple(rest)) 
                  for name,*rest in sorted(r, key=lambda x: (int(x[1]), int(x[2]))))
        return od


print(sailorPerf("test.txt"))
输出:

OrderedDict([('Dennis', ('90', '0')), ('Eva', ('90', '5')),
        ('Alice', ('100', '0')), ('Bob', ('100', '5')),
        ('Clare', ('100', '10'))])
如果您确实不想看到OrderedICT,可以调用
列表(od.items)

对于python2,只需打开包装:

def sailorPerf(filename, header=True):
    with open(filename, mode='r') as csvfile:
        r = csv.reader(csvfile)
        if header==True:
            next(r)
        od = OrderedDict((nm, (mn, std))) 
                  for nm, mn, std in sorted(r,key=lambda x: (int(x[1]),int(x[2]))))
        return od

您需要按转换为int的第二列和第三列对数据进行排序:

from collections import OrderedDict
def sailorPerf(filename, header=True):
    with open(filename, mode='r') as csvfile:
        r = csv.reader(csvfile)
        if header:
            next(r)
        od = OrderedDict((name, tuple(rest)) 
                  for name,*rest in sorted(r, key=lambda x: (int(x[1]), int(x[2]))))
        return od


print(sailorPerf("test.txt"))
输出:

OrderedDict([('Dennis', ('90', '0')), ('Eva', ('90', '5')),
        ('Alice', ('100', '0')), ('Bob', ('100', '5')),
        ('Clare', ('100', '10'))])
如果您确实不想看到OrderedICT,可以调用
列表(od.items)

对于python2,只需打开包装:

def sailorPerf(filename, header=True):
    with open(filename, mode='r') as csvfile:
        r = csv.reader(csvfile)
        if header==True:
            next(r)
        od = OrderedDict((nm, (mn, std))) 
                  for nm, mn, std in sorted(r,key=lambda x: (int(x[1]),int(x[2]))))
        return od

您的订单是如何颠倒的?我不明白“丹尼斯”是怎么排在第一位的。它是基于帆船比赛的评分系统,所以分数越低,你的排名就越高,所以丹尼斯的分数最低,标准差最低,这意味着他是第一位的。我补充了一个答案,你需要根据两列进行排序。你的顺序颠倒了吗?我不明白“丹尼斯”如何排在第一位。它是基于帆船比赛的评分系统,所以分数越低,你的排名就越高,所以丹尼斯的分数最低,标准差最低,这意味着他是第一位。我添加了一个答案,你需要根据两列进行排序