如何将一个整数字符串变成一个整数列表?
How to turn a string of ints into a list of ints?
假设你有这个整数字符串:
5125458993327395245 1108328029959651534 6552608174082565141 3081501567273068441 2414768202934775086
我尝试使用字符串 join()
或 list()
方法,但它会将每个数字分隔成一个数字:
['5', '1', '2', '5', '4', '5', '8', '9', '9', '3', '3', '2', '7', '3', '9', '5', '2', '4', '5']
等
如何制作一个由每个元素组成的列表,例如每个数字由 space 分隔?这里每个数字的大小是19。像这样:
[5125458993327395245, 1108328029959651534, 6552608174082565141, 3081501567273068441, 2414768202934775086]
example_string = "5125458993327395245 1108328029959651534 6552608174082565141 3081501567273068441 2414768202934775086"
example_list = []
for number in example_string.split(" "):
example_list.append(number)
print(example_list)
拆分地图
s = '5125458993327395245 1108328029959651534 6552608174082565141 3081501567273068441 2414768202934775086'
lst = list(map(int, s.split()))
[5125458993327395245,
1108328029959651534,
6552608174082565141,
3081501567273068441,
2414768202934775086]
最好的方法是使用 map() 函数。
string_to_integer = list(map(int, input().split()))
print(string_to_integer)
假设你有这个整数字符串:
5125458993327395245 1108328029959651534 6552608174082565141 3081501567273068441 2414768202934775086
我尝试使用字符串 join()
或 list()
方法,但它会将每个数字分隔成一个数字:
['5', '1', '2', '5', '4', '5', '8', '9', '9', '3', '3', '2', '7', '3', '9', '5', '2', '4', '5']
等
如何制作一个由每个元素组成的列表,例如每个数字由 space 分隔?这里每个数字的大小是19。像这样:
[5125458993327395245, 1108328029959651534, 6552608174082565141, 3081501567273068441, 2414768202934775086]
example_string = "5125458993327395245 1108328029959651534 6552608174082565141 3081501567273068441 2414768202934775086"
example_list = []
for number in example_string.split(" "):
example_list.append(number)
print(example_list)
拆分地图
s = '5125458993327395245 1108328029959651534 6552608174082565141 3081501567273068441 2414768202934775086'
lst = list(map(int, s.split()))
[5125458993327395245,
1108328029959651534,
6552608174082565141,
3081501567273068441,
2414768202934775086]
最好的方法是使用 map() 函数。
string_to_integer = list(map(int, input().split()))
print(string_to_integer)