How to convert strings to integers in Python?

I have a tuple of tuples from a MySQL query, for example:

T1 = (('13', '17', '18', '21', '32'),
      ('07', '11', '13', '14', '28'),
      ('01', '05', '06', '08', '15', '16'))

I would like to convert all string elements to integers and put them back in a list of lists:

T2 = [[13, 17, 18, 21, 32], [7, 11, 13, 14, 28], [1, 5, 6, 8, 15, 16]]

I tried to achieve this with help eval, but so far have not received any decent result.

+396
source share
15 answers

int()- A built-in function of the Python standard for converting strings to integer values. You call it with a string containing a number as an argument, and returns a number converted to an integer:

print (int("1") + 1)

The above prints 2.

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

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

Python 3:

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

:

T2 = [[int(column) for column in row] for row in T1]

([int(column) for column in row]) list int int -able, , row. ([... for row in T1])) , T1.

, - , int. , , .

, . .

T2 = [parse_a_row_of_T1(row) for row in T1]
+28

:

[[int(y) for y in x] for x in T1]
+16

, int( ), float( ), .

+13

, , , .

,

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())

. , , , .

+11
T3=[]

for i in range(0,len(T1)):
    T3.append([])
    for j in range(0,len(T1[i])):
        b=int(T1[i][j])
        T3[i].append(b)

print T3
+10

.

x = "1"

x - , , .

x = int(x)

x 1 , .

, , .

def is_number(var):
    try:
        if var == int(var):
            return True
    except Exception:
        return False

x = "1"

y = "test"

x_test = is_number(x)

print(x_test)

IDLE True, x .

y_test = is_number(y)

print(y_test)

IDLE False, y .

+6

:

t2 = [map(int, list(l)) for l in t1]
+4

Python 3.5.1 , :

c = input('Enter number:')
print (int(float(c)))
print (round(float(c)))

Enter number:  4.7
4
5

.

+4

Python 2:

from functools import partial

map(partial(map, int), T1)

Python 3 , :

list(map(list, map(partial(map, int), T1)))

def oldmap(f, iterable):
    return list(map(f, iterable))

oldmap(partial(oldmap, int), T1)
+3

.

def parse_int(s):
    try:
        res = int(eval(str(s)))
        if type(res) == int:
            return res
    except:
        return

val = parse_int('10')  # Return 10
val = parse_int('0')  # Return 0
val = parse_int('10.5')  # Return 10
val = parse_int('0.0')  # Return 0
val = parse_int('Ten')  # Return None

if val == None:  # True if input value can not be converted
    pass  # Note: Don't use 'if not val:'
+3

, - rows=[map(int, row) for row in rows] . ( map (f, lst), [f (a) a lst].)

Eval - , , - __import__("os").unlink("importantsystemfile"). ( , int() , ).

+1

, , , :

rumpy.random.permutation(x)

. , , .

0
source

You can do something like this:

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 if a.isdigit()) for b in T1)  
print(new_list)  
-1
source
for str in T2 :
    return num(str)

maybe ll work

-6
source

All Articles