Python 试图在列表中排列列表,但无法';不能正确地排列数字

Python 试图在列表中排列列表,但无法';不能正确地排列数字,python,list,python-2.7,sorting,Python,List,Python 2.7,Sorting,好的,我正在处理一个文件,其中包含一系列列表。我试着按照前5个州和后5个州的参与者总数进行排列 import csv from operator import itemgetter #not really using this right now crimefile = open('APExam.txt', 'r') reader = csv.reader(crimefile) allRows = [row for row in reader] L = sorted(allRows,key=la

好的,我正在处理一个文件,其中包含一系列列表。我试着按照前5个州和后5个州的参与者总数进行排列

import csv
from operator import itemgetter #not really using this right now
crimefile = open('APExam.txt', 'r')
reader = csv.reader(crimefile)
allRows = [row for row in reader]
L = sorted(allRows,key=lambda x: x[1])
for item in L:
print item[0],','.join(map(str,item[1:]))
它打印出如下内容:

State  Total #,% passed,%female
Wyoming 0,0,0
New Hampshire 101,76,12
Utah 103,54,4
Mass 1067,67,18
Montana 11,55,0
Iowa 118,80,9
Alabama 126,79,17
Georgia 1261,51,18
Florida 1521,44,20
Illinois 1559,69,13
New Jersey 1582,74,15
Maine 161,67,16
这打印文件的方式与我想要的差不多,但是参与者的总数并不是按从大到小排序的;它通过查看第一个元素进行排序。如何将其更改为更像:

New Jersey 1582,74,15
Illinois 1559,69,13
Florida 1521,44,20
Georgia 1261,51,18
Etc...
第一次在这里问问题,非常感谢您的帮助!:) 此外,我尝试不使用.sort()或find()函数或“in”运算符,例如“if 6 in[5,4,7 6]…”

编辑*:通过更改L

L = sorted(allRows,key=lambda x: int(x[1]),reverse=True)
我已经到了让列表按降序排列的地步:

State  Total #,% passed,%female
California 4964,76,22
Texas 3979,62,23
New York 1858,69,20
Virginia 1655,60,19
Maryland 1629,66,20
New Jersey 1582,74,15
Illinois 1559,69,13
Florida 1521,44,20

现在我似乎不太明白如何根据这些总数只取前5名和后5名…

你搞砸了排序。CSV文件包含文本,而不管该文本可能是什么

L = sorted(allRows,key=lambda x: int(x[1]))

当我把代码改成这样时,它会产生以下错误“L=sorted(allRows,key=lambda x:int(x[1])ValueError:int()的无效文本,以10为基数:'Total#'”啊,好的,谢谢!我想我会试着自己弄清楚剩下的部分,但这很有帮助:)好吧,所以我试了几个小时,让它只给我前5名和后5名,但基本上我尝试的每件事都产生了一个错误,或者只是混乱了列表。。。有什么建议吗?切碎它<代码>L[:5],
L[-5:]