Python 如何按值对字典列表排序
这是我字典目录的摘录Python 如何按值对字典列表排序,python,sorting,dictionary,Python,Sorting,Dictionary,这是我字典目录的摘录 dictionary[pattern_key] = {"key": index_key, "document": index_source, "startPos":index_start, "endPos": index_end} 如何按文档然后按开始操作排序 我试过类似的方法,但不起作用 sorted\u dict=sorted(dictionary,key=itemgetter(pattern\u key[document])) script.py { 'AGACAA
dictionary[pattern_key] = {"key": index_key, "document": index_source, "startPos":index_start, "endPos": index_end}
如何按文档然后按开始操作排序
我试过类似的方法,但不起作用
sorted\u dict=sorted(dictionary,key=itemgetter(pattern\u key[document]))
script.py
{
'AGACAATCTC': {'startPos': '174', 'document': 'source-document01012.txt', 'endPos': '183', 'key': 'AGACAATCTC'},
'GGTCAGACAA': {'startPos': '18', 'document': 'source-document01012.txt', 'endPos': '27', 'key': 'GGTCAGACAA'},
'TAGATGAAGT': {'startPos': '102', 'document': 'source-document01012.txt', 'endPos': '111', 'key': 'TAGATGAAGT'}
}
您可以将内部字典中的条目作为排序后的
的键:
#!/usr/bin/env python
import sys
dictionary = {};
for pattern in sys.stdin:
if "," in pattern:
pattern_key, pattern_source, pattern_start, pattern_end = pattern.strip().split(",")
index_file = open('index.txt', 'r')
for line in index_file:
if "," in line:
index_key, index_source, index_start, index_end = line.strip().split(",")
if pattern_key == index_key:
dictionary[pattern_key] = {"document": index_source, "startPos":index_start, "endPos": index_end}
sorted(dictionary.items(), key = lambda x: (x[1]['document'], int(x[1]['startPos'])))
for k, v in dictionary.items():
print (k, '-->', v)
元组键将首先按第0个元素排序,然后按第1个元素排序,依此类推
注意,这将生成一个元组列表,其中每个元组都是(str,dict)
编辑:
在您的上下文中,正确的实现如下所示:
sorted(dictionary.items(), key = lambda x: (x[1]['document'], int(x[1]['startPos'])))
您可以将内部字典中的条目作为排序后的的键:
#!/usr/bin/env python
import sys
dictionary = {};
for pattern in sys.stdin:
if "," in pattern:
pattern_key, pattern_source, pattern_start, pattern_end = pattern.strip().split(",")
index_file = open('index.txt', 'r')
for line in index_file:
if "," in line:
index_key, index_source, index_start, index_end = line.strip().split(",")
if pattern_key == index_key:
dictionary[pattern_key] = {"document": index_source, "startPos":index_start, "endPos": index_end}
sorted(dictionary.items(), key = lambda x: (x[1]['document'], int(x[1]['startPos'])))
for k, v in dictionary.items():
print (k, '-->', v)
元组键将首先按第0个元素排序,然后按第1个元素排序,依此类推
注意,这将生成一个元组列表,其中每个元组都是(str,dict)
编辑:
在您的上下文中,正确的实现如下所示:
sorted(dictionary.items(), key = lambda x: (x[1]['document'], int(x[1]['startPos'])))
根据所需标准进行排序,然后从排序列表中创建一个新的排序,因为dict
无法按其性质保持排序:
sorted_values = sorted(dictionary.items(), key = lambda x: (x[1]['document'], int(x[1]['startPos'])))
for k, v in sorted_values:
print (k, '-->', v)
根据所需标准进行排序,然后从排序列表中创建一个新的排序,因为dict
无法按其性质保持排序:
sorted_values = sorted(dictionary.items(), key = lambda x: (x[1]['document'], int(x[1]['startPos'])))
for k, v in sorted_values:
print (k, '-->', v)
对于输出,您想要整个dict值,还是只想要键名?['agacaatc','TAGATGAAGT']像那样?我需要按文档对整个输出进行排序,然后开始排序…排序不会更新字典,使其具有排序值。您需要使用sorted()命令返回的(,)元组列表。如果您将元组列表转换为字典,然后使用它,可能会再次扰乱排序顺序。我看不到dict的列表,而是dict的列表。或者我在这里遗漏了什么?如果您希望对字典进行排序,则需要使用collections.OrderedDict
。常规字典不能保证键的顺序。对于输出,您想要整个dict值,还是只需要键名?['agacaatc','TAGATGAAGT']像那样?我需要按文档对整个输出进行排序,然后开始排序…排序不会更新字典,使其具有排序值。您需要使用sorted()命令返回的(,)元组列表。如果您将元组列表转换为字典,然后使用它,可能会再次扰乱排序顺序。我看不到dict的列表,而是dict的列表。或者我在这里遗漏了什么?如果您希望对字典进行排序,则需要使用collections.OrderedDict
。常规字典不保证键的顺序。在示例数据上运行此操作将返回:('ggtcagaca',{'document':'source-document01012.txt','endPos':'27','startPos':'18','key':'ggtcagaca'})('TAGATGAAGT',{'document':'source-document01012.txt','endPos':'111','startPos':'102','key':'tagatgaagat'})('agacatc',{'document':'source-document01012.txt','endPos':'183','startPos':'174','key':'agacaatc'})
。这是按文档
和startPos
排序的。你能举个例子吗,它没有使用“startPos”排序的值是多少?因为我尝试过类似的方法,它对我有效。嗯,奇怪的是,它给了我一个随机输出?('AAAGCTTACA','-->',{'startPos':'132','document':'source-document01012.txt','endPos':'141'}('GGAGAAATCT','-->','startPos':'78','document':'source-document01012.txt','endPos':'87'})('TCGGGAGCAA','-->','startPos':'216','document':'source document01012.txt','endPos':'225'})('cgtttattgt',',{'startPos':'204','document':'source-document01012.txt','endPos':'213'}('TCACGTAGGA','-->','startPos':'234','document':'source-document01012.txt','endPos':'243'})
为了清晰起见,我删除了“key”,不介意“->”不,排序是有效的。你确定其他一切都准备好了吗?我会在你的示例数据返回中发布我的整个脚本运行此命令:('ggtcagaca',{'document':'source-document01012.txt','endPos':'27','startPos':'18','key':'ggtcagaca'})('tagatgagt',{'document':'source-document01012.txt','endPos':'111','startPos':'102','key':'tagatgagt'})('agacaatc',{'document':'source-document01012.txt','endPos':'183','startPos':'174','key':'agacaatc})
。这是按文档
和startPos
排序的。你能举个例子吗,它没有使用“startPos”排序的值是多少?因为我尝试过类似的方法,它对我有效。嗯,奇怪的是,它给了我一个随机输出?('AAAGCTTACA','-->',{'startPos':'132','document':'source-document01012.txt','endPos':'141'}('GGAGAAATCT','-->','startPos':'78','document':'source-document01012.txt','endPos':'87'})('TCGGGAGCAA','-->','startPos':'216','document':'source document01012.txt','endPos':'225'})('cgtttattgt',',{'startPos':'204','document':'source-document01012.txt','endPos':'213'}('TCACGTAGGA','-->','startPos':'234','document':'source-document01012.txt','endPos':'243})
为了清晰起见,我删除了“key”,不介意“->”排序工作。你确定其他一切都准备就绪并且正确吗?我将发布我的整个脚本