TypeError:不可排序的类型:dict()<dict()

时间:2019-05-28 07:35:19

标签: python dictionary

我对python完全陌生,我有一个脚本会产生此错误

for a in sorted([p for p in PRODUCERS.Get('PartitionName="{}"'.format(partition)) 
                 if p["SignalName"] not in EXCLUDED_SIGNALS]):
TypeError: unorderable types: dict() < dict()

该脚本是使用python 2.7制作的,我使用的是3.5(32位)。是否发生了某些变化?

功能如下:

def tests_init(partition, dll, fce_name, limit_skip, limit_show, outputs):

all_inputs = {}
all_outputs = {}

# prepare inputs and outputs
for a in sorted([p for p in PRODUCERS.Get('PartitionName="{}"'.format(partition)) if p["SignalName"] not in EXCLUDED_SIGNALS]):
    all_inputs["{}__{}".format(a["ProducerFunction"],a["SignalName"])] = a["SignalType"]
for a in sorted([c for c in CONSUMERS.Get('PartitionName="{}"'.format(partition)) if c["SignalName"] not in EXCLUDED_SIGNALS]):
    all_outputs["{}__{}".format(a["ConsumerFunction"],a["SignalName"])] = a["SignalType"]

all_outputs["InvalidRdResult"] = "uint32"
all_outputs["InvalidRd4Result"] = "uint32"
all_outputs["InvalidWrResult"] = "uint32"
all_outputs["InvalidWr4Result"] = "uint32"

# init test
order_dict, help_out = test_init(all_inputs, all_outputs, partition, outputs, limit_skip, limit_show)

all_outputs = {o:all_outputs[o] for o in all_outputs if o in help_out}

file_desc =  "Testing Ports initialization and functions"
file_desc += "\n;                    portDclrRd()/portDclrRd4()/portDclrWr()/portDclrWr4()"
file_desc += "\n;                    for invalid signal name input"
file_desc += "\n;                    Initial values for port signals are defined in Input Tables"
file_desc += "\n;                    in related SRDD/DRD sections where the signals are produced."
if(partition=="fcm"):
    file_desc += "\n;                    This file contains {} half of output signals".format({0:"first", 1:"second"}[int(limit_skip/limit_show)])
    file_desc += "\n;                    Files are divided due to line length limitations in CTP tool"

1 个答案:

答案 0 :(得分:0)

字典排序列表的示例:

在示例中,键为“ id”字段。您需要确定要使用的适当字段。

v2 engine

输出

lst = [{'name':'jack','id':12},{'name':'sam','id':4},{'name':'ben','id':152}]
print(sorted(lst,key = lambda i: i['id']))