首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python sorted()函数并返回多个值

Python sorted()函数并返回多个值
EN

Stack Overflow用户
提问于 2012-11-12 00:03:23
回答 1查看 186关注 0票数 0

我正在尝试将Python程序转换为C#。我不明白这里在做什么。

代码语言:javascript
复制
def mincost(alg):
    parts = alg.split(' ')
    return sorted([cost(0, parts, 'G0 '),cost(1, parts, 'G1 ')], key=operator.itemgetter(1))[0]

def cost(grip, alg, p = '', c = 0.0, rh = True):
    if (len(alg) == 0):
        return (postProcess(p),c)

postprocess返回一个字符串

cost返回在sorted()函数上使用的多个参数?sorted()函数如何使用这些多个值?

key=operator.itemgetter(1)是做什么的?这是排序的基础吗,因此在这种情况下,cost的多值返回将使用c的值

在C#中有没有办法做到这一点?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-11-12 01:25:02

sorted的用法有点奇怪。您可以很容易地将其替换为一个简单的if语句。更奇怪的是,cost只返回c作为返回元组的第二个值。在mincost中,调用cost时不会使用非默认值c的值,因此c始终为0.0,这使得排序变得非常多余。但是我猜关于成本函数有一些缺失的部分。

不过,您可以像这样实现它的函数:

代码语言:javascript
复制
string MinCost (string alg) {
    List<string> parts = alg.split(" ");
    Tuple<string, double> cost1 = Cost(0, parts, "G0 ");
    Tuple<string, double> cost2 = Cost(1, parts, "G1 ");

    if (cost1[1] < cost2[1])
        return cost1[0];
    else
        return cost2[0];
}

Tuple<string, double> Cost (int grip, List<string> alg, string p="", double c=0.0, bool rh=True) {
    if (alg.Count == 0)
        return new Tuple<string, double>(PostProcess(p), c);

    // ... there should be more here
}

(未测试)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13332872

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档