“Konversi daftar string ke ints python” Kode Jawaban

Konversi daftar string ke ints python

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

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

Ubah Daftar String ke Int List 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

string python ke daftar 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

Daftar string ke angka python

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

string python ke daftar int

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

Python Convert List Daftar String ke 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

Jawaban yang mirip dengan “Konversi daftar string ke ints python”

Pertanyaan yang mirip dengan “Konversi daftar string ke ints python”

Lebih banyak jawaban terkait untuk “Konversi daftar string ke ints python” di Python

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya