【问题标题】:Python getting an error while sorting a dictionaryPython在对字典进行排序时出错
【发布时间】:2021-11-14 00:44:39
【问题描述】:

有一阵子没用过 Python 了。在对字典进行排序时遇到了一些问题

我有一本名为 bad_functions 的字典,里面有大约 500 对

示例:{'lambda_1': 0, 'lambda_2': 0,, 'lambda_2': "unreserved}

代码:

from contextlib import redirect_stdout
import subprocess
import json

# deafult region is us-east-2
region="us-east-2"

# command for getting all lambdas lambdas in eu-east-2 
list_lambda_command = "aws lambda list-functions --region {region} --query 'Functions[].FunctionName' --output json"
# command for getting a specific lambda cuoncurrency  
lambda_concurrency="aws lambda get-function-concurrency --function-name {function_name}"

# running the first aws cli command on host and load to list object 
lambda_json_output = subprocess.check_output(['bash','-c', list_lambda_command.format(region=region)]).decode()
lambda_list=json.loads(lambda_json_output)

# empty list for incoming 'bad' lambda's results
bad_functions={}

for function in lambda_list: 
    # getting lambda funcations with unreserved concurreny value into a dict
    try:
        output=subprocess.check_output(['bash','-c', lambda_concurrency.format(function_name=function)]).decode()
        current_lambda_concurrency=json.loads(output)
    except:
        bad_functions[f'{function}']="unreserved"
        continue

    # getting lambda funcations with 'bad' concurreny value value into a dict
    current_concurrency=current_lambda_concurrency['ReservedConcurrentExecutions']
    if current_concurrency != 1:
        bad_functions[f'{function}']=int(current_concurrency)

# sorting the dic by value
print(bad_functions)
bad_functions_sorted={k: v for k, v in sorted(bad_functions.items(), key=lambda item: item[1])}

我得到错误:

File "script.py", line 40, in <module>
    bad_functions_sorted={k: v for k, v in sorted(bad_functions.items(), key=lambda item: item[1])}
TypeError: '<' not supported between instances of 'str' and 'int'

【问题讨论】:

  • 提供的示例没有产生提供的错误。请提供适当的示例,以便我们重现该问题。
  • 建议:将key=lambda item: item[1] 替换为key=lambda item: str(item[1])。但我不能保证这会按照你想要的顺序排序。
  • 嗯,我了解 Capie,但 dict 信息有点私密。我应该如何给出这样的例子?
  • 更新了我的帖子以使用整个代码..我希望现在更容易理解

标签: python python-3.x sorting dictionary lambda


【解决方案1】:

我的错,问题中没有提供足够的信息。

可能的输出可能是

{'lambda_1': 0, 'lambda_2': 0,, 'lambda_2': "unreserved}

所以 ERROR TypeError: '&lt;' not supported between instances of 'str' and 'int' 很有意义

我为解决这个问题所做的是将它们分成 2 个不同的字典

for function in lambda_list: 

    try:
        output=subprocess.check_output(['bash','-c', lambda_concurrency.format(function_name=function)]).decode()
        current_lambda_concurrency=json.loads(output)
    except:
        unreserved_function[f'{function}']="unreserved"
        continue

  current_concurrency=current_lambda_concurrency['ReservedConcurrentExecutions']
    if current_concurrency != 1:
        concurrency_functions[f'{function}']=int(current_concurrency)

并且只对 'int' 字典进行排序

bad_functions_sorted={k: v for k, v in sorted(concurrency_functions.items(), key=lambda item: item[1])}

【讨论】:

    猜你喜欢
    • 2023-04-05
    • 1970-01-01
    • 2017-07-12
    • 1970-01-01
    • 2014-11-10
    • 2016-06-08
    • 2021-04-17
    • 2021-12-21
    • 1970-01-01
    相关资源
    最近更新 更多