“Python przekonwertuje listę ciągów na listę liczb całkowitych” Kod odpowiedzi

Konwertuj listę ciągów na Ints Python

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

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

Python przekonwertuje listę ciągów na listę liczb całkowitych

# Basic syntax:
list_of_ints = [int(i) for i in list_of_strings]

# Example usage with list comprehension:
list_of_strings = ['2', '3', '47']
list_of_ints = [int(i) for i in list_of_strings]
print(list_of_ints)
--> [2, 3, 47]
Charles-Alexandre Roy

Zmień listę ciągów na list Int 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

Python przekonwertuje listę list ciągów na int 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

Odpowiedzi podobne do “Python przekonwertuje listę ciągów na listę liczb całkowitych”

Pytania podobne do “Python przekonwertuje listę ciągów na listę liczb całkowitych”

Więcej pokrewnych odpowiedzi na “Python przekonwertuje listę ciągów na listę liczb całkowitych” w Python

Przeglądaj popularne odpowiedzi na kod według języka

Przeglądaj inne języki kodu