“Konwertuj listę ciągów na Ints Python” 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

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

ciąg Python do listy 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

Lista ciągów do liczb Python

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

ciąg Python do listy int

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

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 “Konwertuj listę ciągów na Ints Python”

Pytania podobne do “Konwertuj listę ciągów na Ints Python”

Więcej pokrewnych odpowiedzi na “Konwertuj listę ciągów na Ints Python” w Python

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

Przeglądaj inne języki kodu