【问题标题】:ValueError: invalid literal for int() with base 10: for map(int, list) in Python errorValueError: int() 以 10 为底的无效文字: for map(int, list) in Python 错误
【发布时间】: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") 实现了什么? mapint 函数应用于列表的所有元素。如果其中有非整数,则需要捕获产生的错误并进行处理。
  • 想要如何处理这个问题?扔掉"and"?返回占位符值?
  • 有很多方法可以处理,包括忽略元素,用默认值替换结果,或者从输入中完全消除它。 必须定义所需的操作是什么;我们可以帮你编码,但不能选择它。
  • 我要将它附加到 gates[] 数组 [gates.append((gate, ports))],所以我的最终 gates[] 将具有这样的值 [('and' , [2, 3, 1])]

标签: python python-3.x dictionary valueerror


【解决方案1】:
# Lets do a lambda function to solve the problem and add a condition
# if there is a int value in bits list it will be added too

bits    = ['and', '2', '3', '1']    
intplus = lambda i,p: \
              (isinstance(i,str) and i.isdigit() and int(i)+p) or \
              (isinstance(i,int) and i+p) or i
ports   = [ intplus(p, _plusnet) for p in bits ]

# bits = ['and', '2', '3', '1', 5] will work too and 5 will be added

【讨论】:

    【解决方案2】:
    bits = ['and', '2', '3', '1']
    ports = [int(s) if s.isdigit() else s for s in bits]
    #OP request, get tuple
    ports = ((ports[0], [port for port in ports[1:]]))
    

    使用列表推导检查您的列表元素是否符合int 类型。如果是,请int(),否则不要。

    或者,删除不符合条件的元素:

    bits = ['and', '2', '3', '1']
    ports = [int(s) for s in bits if s.isdigit()]  
    #The below line is for the next iteration, where plus_net can be considered a counter
    ports = [p+_plusnet for p in ports]
    

    【讨论】:

    • ports = [int(s) if s.isdigit() for s in bits] ^ SyntaxError: invalid syntax...得到这个错误
    • 对于第一个逻辑,您将在 ports = [p+_plusnet for p in ports] cannot concatenate 'str' 和 'int' objects 处收到错误
    • 是的,我更正了。对于您评论的第二部分,这是您要质疑的逻辑。您不能连接 str 和 int 对象;)。你想要什么?
    • 我将把它附加到一个 gates[] 数组 [gates.append((gate, ports))],所以我最终的 gates[] 将具有这样的值 [('and', [2, 3, 1])]
    • 好的,你会得到一个比特列表的列表,还是你会使用简单的列表,例如 [str, int...]。换句话说,你在门中的元组元素总是 str 索引 0 和接下来来自端口的整数列表吗?
    【解决方案3】:
    bits = ['and', '2', '3', '1']
    
    def myint(a):
        if a.isdigit():
            return int(a)
        else:
            return 0
    ports = map(myint, bits)
    ports = [p+_plusnet for p in ports]
    

    如果你想替换字符串 litral

    【讨论】:

    • 在这种情况下我会得到一个额外的 0 对吗?我的结果是 [('and', [0, 2, 3, 1])]
    • 基本上我想删除'and'并只返回[2,3,1]
    【解决方案4】:

    map(fun, iterable) => fun 将应用于 iterable 的每个元素。 并且按照这个逻辑。

    地图(整数位) int('and') 将为 int() 以 10 为基数抛出无效的文字,因为您不能直接将字母文字转换为 int。

    【讨论】:

    • bits = ['and', '2', '3', '1'] def myint(a): if a.isdigit(): return int(a) else: return 0 个端口= map(myint, bits) ports = [p+_plusnet for p in ports]
    猜你喜欢
    • 2020-08-09
    • 2016-10-17
    • 2018-09-09
    • 2020-01-04
    • 2010-12-22
    • 2011-07-07
    • 2019-10-14
    相关资源
    最近更新 更多