按第一项对嵌套列表进行排序——itemgetter 没有做到这一点 [英] Sorting a nesting list by the first item -- itemgetter not doing the trick

查看:18
本文介绍了按第一项对嵌套列表进行排序——itemgetter 没有做到这一点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一本已转换为列表的字典,因此我可以按第一项进行排序.字典中的键是一个字符串(数字),值是一个整数,保存在列表中.
字典转换的列表如下所示:

I have a dictionary that I've converted to a list so I can sort by the first item. The key in the dictionary is a string (of numbers), the value is an integer which is maintained in the list.
The list from the dictionary conversion looks like:

[('228055', 1), ('228054', 1), ('228057', 2), ('228056', 1), ('228051', 1), ('228050', 1),     ('228053', 1), ('203184', 6), ('228059', 1), ('228058', 1), ('89370', 2), ('89371', 3), ('89372', 2), ('89373', 1), ('89374', 1), ('89375', 1), ('89376', 1), ('89377', 1), ('89378', 1), ('89379', 1),.........]

字典中有大约 240,000 项.我想按第一个索引对字典进行排序,但是当我使用 itemgetter(0) 时,它首先按所有1"对列表进行排序.排序后的列表看起来像:

There are around 240,000 items in the dictionary. I would like to sort the dictionary by the first index, but when I use itemgetter(0) it sorts the list by all the "1's" first. The sorted listed looks like:

[('0', 3), ('1', 3), ('10', 3), ('100', 4), ('1000', 3), ('10000', 1), ('100000', 3), ('100001', 2), ('100002', 3), ('100003', 3), ('100004', 2), ('100005', 2), ('100006', 2), ('100007', 2), ('100008', 2), ('100009', 2), ('10001', 1), ('100010', 3), ('100011', 3), ('100012', 3), ('100013', 2), ('100014', 1), ('100015', 1), ('100016', 1), ('100017', 1), ('100018', 1), ....]

我希望列表按 ['0', 3), ('1', 3), ('2', integer), ('3', integer),...('240,000', 整数)]

I would like the list to be sorted by ['0', 3), ('1', 3), ('2', integer), ('3', integer),...('240,000', integer)]

这是我的代码,我将文本文件读取到字典,转换为列表并使用 itemgetter 按嵌套列表中的第一项排序.我需要代码中的字典,因为我严重依赖它来按键查找值.我只是尝试在所有进程都运行后对输出文件的字典进行排序.感谢您的帮助.

Here's my code where I'm reading in a text file to a dictionary, converting to a list and used itemgetter to sort by first item in nested list. I need the dictionary in the code because I heavily depend on it to look up values by the key. I'm only trying to sort the dictionary for the output file once all the processes are ran. Thanks for any help.

import sys, string, csv, arcpy, os, fileinput, traceback
from arcpy import env
from operator import itemgetter


#Creating a dictionary of FID: LU_Codes from external txt file
text_file = open("H:SWATNCFID_Whole_Copy.txt", "rb")
#Lines = text_file.readlines()
FID_GC_dict =  dict()
reader = csv.reader(text_file, delimiter='	')
for line in reader:
    FID_GC_dict[line[0]] = int(line[1])
text_file.close()

dict_List = [(x, FID_GC_dict[x]) for x in FID_GC_dict.keys()]
dict_List.sort(key=itemgetter(0))
print dict_List

推荐答案

更改键以将字符串转换为 int 会对您有所帮助,此外还有一些其他排序技巧.

Changing the key to convert the string to an int will help you, also here are some other sorting tips.

from operator import itemgetter

list_to_sort=[('89372', 2), ('89373', 1), ('89374', 1), ('89375', 1), ('89376', 1),     ('89377', 1), ('228055', 1), ('228054', 1), ('228057', 2), ('228056', 1), ('228051', 1), ('228050', 1),('228053', 1), ('203184', 6), ('228059', 1), ('228058', 1), ('89370', 2), ('89371', 3), ('89372', 2), ('89373', 1), ('89374', 1), ('89375', 1), ('89376', 1), ('89377', 1)]
print list_to_sort

list_to_sort.sort()
print list_to_sort # badly sorted as described

list_to_sort.sort(key=itemgetter(0))
print list_to_sort # badly sorted as described (same as above)

list_to_sort.sort(key=lambda x: int(x[0]))
print list_to_sort # sorted well

list_to_sort.sort(key=lambda x: int(x[0]), reverse=True)
print list_to_sort # sorted well in reverse

关于构建列表以从字典中排序的旁注.iteritems() 是一种更好的方式来完成您对以下内容的操作

Side note on building the list to sort from the dict. iteritems() is a nicer way of doing what you do with the following

dict_List = [(x, FID_GC_dict[x]) for x in FID_GC_dict.keys()]

dict_List = [(k,v) for k,v in FID_GC_dict.iteritems()]

这篇关于按第一项对嵌套列表进行排序——itemgetter 没有做到这一点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆