How to convert strings into integers in Python?

后端 未结 15 2160
自闭症患者
自闭症患者 2020-11-22 01:33

I have a tuple of tuples from a MySQL query like this:

T1 = ((\'13\', \'17\', \'18\', \'21\', \'32\'),
      (\'07\', \'11\', \'13\', \'14\', \'28\'),
               


        
15条回答
  •  深忆病人
    2020-11-22 01:51

    int() is the Python standard built-in function to convert a string into an integer value. You call it with a string containing a number as the argument, and it returns the number converted to an integer:

    print (int("1") + 1)
    

    The above prints 2.

    If you know the structure of your list, T1 (that it simply contains lists, only one level), you could do this in Python 2:

    T2 = [map(int, x) for x in T1]
    

    In Python 3:

    T2 = [list(map(int, x)) for x in T1]
    

提交回复
热议问题