Python程序将整数列表转换为字符串列表
给定一个整数列表。任务是将它们转换为字符串列表。
例子:
Input: [1, 12, 15, 21, 131]
Output: ['1', '12', '15', '21', '131']
Input: [0, 1, 11, 15, 58]
Output: ['0', '1', '11', '15', '58']
方法一:使用map()
# Python code to convert list of
# string into sorted list of integer
# List initialization
list_int = [1, 12, 15, 21, 131]
# mapping
list_string = map(str, list_int)
# Printing sorted list of integers
print(list(list_string))
输出:
['1', '12', '15', '21', '131']
示例 2:使用列表推导
# Python code to convert list of
# string into sorted list of integer
# List initialization
list_string = [1, 12, 15, 21, 131]
# Using list comprehension
output = [str(x) for x in list_string]
# Printing output
print(output)
输出:
['1', '12', '15', '21', '131']
方法3:使用迭代
# Python code to convert list of
# string into sorted list of integer
# List initialization
list_string = [1, 12, 15, 21, 131]
list_int = []
# using iteration and sorted()
for i in list_string:
list_int.append(i)
# printing output
print(list_int)
输出:
['1', '12', '15', '21', '131']