Python|将字符串列表转换为整数的排序列表
给定一个字符串列表,编写一个Python程序将其转换为已排序的整数列表。
例子:
Input: ['21', '1', '131', '12', '15']
Output: [1, 12, 15, 21, 131]
Input: ['11', '1', '58', '15', '0']
Output: [0, 1, 11, 15, 58]
让我们讨论一下我们可以完成这项任务的不同方法。
方法 #1:使用map
和sorted()
# Python code to convert list of
# string into sorted list of integer
# List initialization
list_string = ['21', '1', '131', '12', '15']
# mapping
list_map = map(int, list_string)
# sorting list
list_sorted = sorted(list_map)
# Printing sorted list of integers
print(list_sorted)
输出:
[1, 12, 15, 21, 131]
方法 #2:使用列表推导
# Python code to convert list of
# string into sorted list of integer
# List initialization
list_string = ['11', '1', '58', '15', '0']
# Using list comprehension
output = [int(x) for x in list_string]
# using sort function
output.sort()
# Printing output
print(output)
输出:
[0, 1, 11, 15, 58]
方法#3:使用迭代
# Python code to convert list of
# string into sorted list of integer
# List initialization
list_string = ['11', '1', '58', '15', '0']
# using iteration and sorted()
list_sorted = sorted(int(x) for x in list_string)
# printing output
print(list_sorted)
输出:
[0, 1, 11, 15, 58]