代码之家  ›  专栏  ›  技术社区  ›  Md Rafee

将整数列表转换为整数python

  •  1
  • Md Rafee  · 技术社区  · 7 年前

    lists = [8,7,2]
    

    result = 872
    

    result = 0
    for count, i in enumerate(reversed(lists)):
        result += i*(10**count)
    

    提前谢谢

    4 回复  |  直到 7 年前
        1
  •  3
  •   Vardan    7 年前

    你可以试试这个

    int("".join(list(map(str,lists))))
    

        2
  •  4
  •   Umang Gupta    7 年前

    from functools import reduce
    reduce(lambda x,y: 10*x+y, (lists))
    

    无需转换为字符串并返回。

    from functools import reduce
    def combine(x,y):
        temp =y
        if (temp==0): 
            return x*10
        while(temp>0):
            x*=10
            temp//=10
        return x + y
    
    reduce(combine,lists)
    
        3
  •  2
  •   PydPiper    7 年前

    你也可以把绳子剥皮:

    lists=[8, 7, 2]
    result = int((str(lists)).strip("[]").replace(", ",""))
    
        4
  •  1
  •   antfuentes87    7 年前

    lists = [8,7,2]
    result = int("".join([str(l) for l in lists]))