【发布时间】:2021-04-17 18:44:00
【问题描述】:
我正在尝试学习python中map()的功能
现在当我这样做时,
bits = ['and', '2', '3', '1']
ports = map(int, bits)
#The below line is for the next iteration, where plus_net can be considered a counter
ports = [p+_plusnet for p in ports]
现在 bits 是一个列表,当我尝试 map() 沿端口迭代时,我得到了错误,
ValueError: int() 以 10 为底的无效文字:'and'
不知道如何处理。 建议? 谢谢!
【问题讨论】:
-
您认为
something = int("and")实现了什么?map将int函数应用于列表的所有元素。如果其中有非整数,则需要捕获产生的错误并进行处理。 -
您想要如何处理这个问题?扔掉
"and"?返回占位符值? -
有很多方法可以处理,包括忽略元素,用默认值替换结果,或者从输入中完全消除它。 您必须定义所需的操作是什么;我们可以帮你编码,但不能选择它。
-
我要将它附加到 gates[] 数组 [gates.append((gate, ports))],所以我的最终 gates[] 将具有这样的值 [('and' , [2, 3, 1])]
标签: python python-3.x dictionary valueerror