Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Most pythonic way to port this tuple unpacking with lambda from Python 2 into Python 3

Tags:

python

lambda

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))

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

like image 856
Filipe Avatar asked Sep 04 '26 23:09

Filipe


2 Answers

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)]
like image 148
Francisco Avatar answered Sep 06 '26 11:09

Francisco


If you prefer functional style there is:

from operator import eq
from itertools import starmap

accs.extend(starmap(eq, zip(lab, lab_pred)))
like image 39
Paul Panzer Avatar answered Sep 06 '26 11:09

Paul Panzer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!