将带有lambda的元组解包从Python 2移植到Python 3的大多数pythonic方式 [英] Most pythonic way to port this tuple unpacking with lambda from Python 2 into Python 3

查看:97
本文介绍了将带有lambda的元组解包从Python 2移植到Python 3的大多数pythonic方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下Python 2代码,用于在lambda内部解压缩元组.该lambda包含在for循环中.

I have the following Python 2 code which unpacks a tuple inside a lambda. This lambda is contained inside a for loop.

    for lab, lab_pred, length in zip(labels, labels_pred, sequence_lengths):
        accs += map(lambda (a, b): a == b, zip(lab, lab_pred))

将其移植到Python 3中的最佳方法是什么?

What is the best way to port this into Python 3?

推荐答案

我认为最好的解决方案是不使用maplambda,而是使用列表理解:

I think the best solution would be to not use map and lambda, use a list comprehension instead:

accs += [a == b for a, b in zip(lab, lab_pred)]

这篇关于将带有lambda的元组解包从Python 2移植到Python 3的大多数pythonic方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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