How to convert strings into integers in Python?

后端 未结 15 2243
自闭症患者
自闭症患者 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:58

    I would agree with everyones answers so far but the problem is is that if you do not have all integers they will crash.

    If you wanted to exclude non-integers then

    T1 = (('13', '17', '18', '21', '32'),
          ('07', '11', '13', '14', '28'),
          ('01', '05', '06', '08', '15', '16'))
    new_list = list(list(int(a) for a in b) for b in T1 if a.isdigit())
    

    This yields only actual digits. The reason I don't use direct list comprehensions is because list comprehension leaks their internal variables.

提交回复
热议问题