python2中处理map函数&蟒蛇3 [英] Handling map function in python2 & python3

查看:56
本文介绍了python2中处理map函数&蟒蛇3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我遇到了一个问题&与可能的解决方案混淆,代码部分是

//结果阅读器中的代码部分结果 = 地图(整数,输入())//消费者调用结果消费者(结果)

这与它们如何工作无关,问题是当您在 python2 中运行时,它会在结果获取部分引发异常,因此结果读取器可以处理异常,但是如果 python3 返回一个 map 对象,因此只有消费者才能处理异常.有没有保持 map 功能的解决方案?处理 python2 中的异常 &python3

python3

<预><代码>>>>d = 地图(整数,输入())1,2,3,a>>>d<0x7f70b11ee518 处的映射对象>>>>

python2

<预><代码>>>>d = 地图(整数,输入())1,2,3,'a'回溯(最近一次调用最后一次):文件<stdin>",第 1 行,位于 <module>ValueError:int() 的无效文字,基数为 10:'a'>>>

解决方案

map 的行为不仅仅是python2和python3的区别,input也是区别,您需要记住两者之间的基本区别,以使代码兼容两者

python 3 vs python 2地图 = itertools.imapzip = itertools.izip过滤器 = itertools.ifilter范围 = x 范围输入 = 原始输入

所以要为两者编写代码,您可以使用列表理解等替代方法,它们对两者都有效,对于那些没有简单替代方法的替代方法,您可以创建新函数和/或使用条件重命名,例如

my_input = 输入尝试:原始输入除了 NameError: #we are in python 3my_input = lambda msj=None: eval(input(msj))

(或者用你最喜欢的方式来检查正在执行哪个版本的python)

# 结果阅读器中的代码部分结果 = [ int(x) for x in my_input() ]# 消费者调用结果消费者(结果)

这样,无论您运行哪个版本的 Python,您的代码都会执行相同的操作.

但正如 jsbueno 提到的,eval 和 python2 的 input危险 所以使用更安全的 raw_input 或 python3 的 input

尝试:输入 = 原始输入除了 NameError: #we are in python 3经过

(或者用你最喜欢的方式来检查正在执行哪个版本的python)

然后如果您的计划是将您的输入提供为 1,2,3 添加适当的拆分

# 结果阅读器中的代码部分结果 = [ int(x) for x in input().split(",") ]# 消费者调用结果消费者(结果)

Recently i came across a question & confused with a possible solution, code part is

// code part in result reader
result = map(int, input())
// consumer call
result_consumer(result)

its not about how do they work, the problem is when you are running in python2 it will raise an exception, on result fetching part, so result reader can handle the exception, but incase of python3 a map object is returned, so only consumer will be able to handle exception. is there any solution keeping map function & handle the exception in python2 & python3

python3

>>> d = map(int, input())
1,2,3,a
>>> d
<map object at 0x7f70b11ee518>
>>> 

python2

>>> d = map(int, input())
1,2,3,'a'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'a'
>>> 

解决方案

the behavior of map is not the only difference between python2 and python3, input is also difference, you need to keep in mind the basic differences between the two to make code compatible for both

python 3 vs python 2
  map    =   itertools.imap
  zip    =   itertools.izip
  filter =   itertools.ifilter
  range  =   xrange
  input  =   raw_input

so to make code for both, you can use alternatives like list comprehension that work the same for both, and for those that don't have easy alternatives, you can make new functions and/or use conditional renames, like for example

my_input = input
try: 
    raw_input
except NameError: #we are in python 3
    my_input = lambda msj=None: eval(input(msj))

(or with your favorite way to check which version of python is in execution)

# code part in result reader
result = [ int(x) for x in my_input() ]
# consumer call
result_consumer(result)

that way your code do the same regardless of which version of python you run it.

But as jsbueno mentioned, eval and python2's input are dangerous so use the more secure raw_input or python3's input

try: 
    input = raw_input
except NameError: #we are in python 3
    pass

(or with your favorite way to check which version of python is in execution)

then if your plan is to provide your input as 1,2,3 add an appropriate split

# code part in result reader
result = [ int(x) for x in input().split(",") ]
# consumer call
result_consumer(result)

这篇关于python2中处理map函数&amp;蟒蛇3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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