简体   繁体   中英

map versus list comprehension in function return

Simple question:

Matplotlib has a function that returns two values:

locs,label = plt.yticks()

Pylint complains about this line, telling me "Used builtin function map". So I went to pylint site and found this: http://pylint-messages.wikidot.com/messages:w0141 So, I'm trying to fix this warning using list comprehension. But what is the correct form?

[(locs,label) for plt.yticks()]

is not working.

Thanks!

EDIT: I made a simple test script to try to show the problem, and the problem was one line below:

#!/usr/bin/python

""" docstring """

import matplotlib.pyplot as plt

LOCS, LABEL = plt.yticks()

plt.yticks(LOCS, map(lambda x: "%.2f" % x, LOCS)) # offending line

print(LOCS)

So duh, I was looking on the wrong line. How this lambda can be adjusted to list comprehension? Thanks

locs,label = plt.yticks()

Is the correct way to receive two variables from a function. You could receive it as a single variable, and work with the tuple object, but that would be pointless.

It's possible that pylint is complaining about plt.yticks . Apart from it being out of your control, it is not in general preferable to use a list comprehension instead of map , just in a wide variety of cases.

In the case of plt.yticks(LOCS, map(lambda x: "%.2f" % x, LOCS)) , a list comprehension is likely to be more readable, and may be faster.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM