“convertir la liste des chaînes en ints python” Réponses codées

convertir la liste des chaînes en ints python

test_list = ['1', '4', '3', '6', '7'] 

int_list = [int(i) for i in test_list]
EDestroyer

Modifier la liste des chaînes vers int la liste Python

Use the map function (in Python 2.x):
results = map(int, results)

In Python 3, you will need to convert the result from map to a list:
results = list(map(int, results))
Spotless Squirrel

chaîne python à la liste d'int

>>> example_string = '0, 0, 0, 11, 0, 0, 0, 0, 0, 19, 0, 9, 0, 0, 0, 0, 0, 0, 11'
>>> list(map(int, example_string.split(',')))  # Python 3, in Python 2 the list() call is redundant
[0, 0, 0, 11, 0, 0, 0, 0, 0, 19, 0, 9, 0, 0, 0, 0, 0, 0, 11]
>>> [int(s) for s in example_string.split(',')]
[0, 0, 0, 11, 0, 0, 0, 0, 0, 19, 0, 9, 0, 0, 0, 0, 0, 0, 11]
Unusual Unicorn

Liste des chaînes aux nombres Python

test_list = ['1', '4', '3', '6', '7']
test_list = list(map(int, test_list))
WIP

chaîne python à la liste d'int

[int(s) for s in example_string.split(',')]
Unusual Unicorn

Python convertit la liste des listes de chaînes en int

# Example usage using list comprehension:
# Say you have the following list of lists of strings and want integers
x = [['565.0', '575.0'], ['1215.0', '245.0'], ['1740.0', '245.0']]
list_of_integers = [[int(float(j)) for j in i] for i in x]

print(list_of_integers)
--> [[565, 575], [1215, 245], [1740, 245]]

# Note, if the strings don't have decimals, you can omit float()
Charles-Alexandre Roy

Réponses similaires à “convertir la liste des chaînes en ints python”

Questions similaires à “convertir la liste des chaînes en ints python”

Plus de réponses similaires à “convertir la liste des chaînes en ints python” dans Python

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code