Python|将数字字符串转换为混合列表中的整数
有时,在处理数据时,我们可能会遇到一个问题,即我们接收到混合数据,需要将字符串形式的整数元素转换为整数。在数据预处理步骤中可能需要这种操作。让我们讨论可以执行此任务的某些方式。
方法 #1:使用列表理解 + isdigit()
这是可以执行此任务的一种方式。在此,我们使用 isdigit 检查字符串的每个元素是否为数字,如果为一则转换为 int。迭代使用列表理解。
Python3
# Python3 code to demonstrate working of
# Convert String numbers to integers in mixed List
# using list comprehension + isdigit()
# initialize list
test_list = ["gfg", "1", "is", "6", "best"]
# printing original list
print("The original list : " + str(test_list))
# Convert String numbers to integers in mixed List
# using list comprehension + isdigit()
res = [int(ele) if ele.isdigit() else ele for ele in test_list]
# printing result
print("List after converting string numbers to integers : " + str(res))
Python3
# Python3 code to demonstrate working of
# Convert String numbers to integers in mixed List
# using map() + lambda + isdigit()
# initialize list
test_list = ["gfg", "1", "is", "6", "best"]
# printing original list
print("The original list : " + str(test_list))
# Convert String numbers to integers in mixed List
# using map() + lambda + isdigit()
res = list(map(lambda ele : int(ele) if ele.isdigit()
else ele, test_list))
# printing result
print("List after converting string numbers to integers : " + str(res))
输出 :
The original list : ['gfg', '1', 'is', '6', 'best']
List after converting string numbers to integers : ['gfg', 1, 'is', 6, 'best']
方法 #2:使用 map() + lambda + isdigit()
也可以使用上述功能执行此任务。在此,我们使用 map() 和 lambda 函数执行迭代任务。
Python3
# Python3 code to demonstrate working of
# Convert String numbers to integers in mixed List
# using map() + lambda + isdigit()
# initialize list
test_list = ["gfg", "1", "is", "6", "best"]
# printing original list
print("The original list : " + str(test_list))
# Convert String numbers to integers in mixed List
# using map() + lambda + isdigit()
res = list(map(lambda ele : int(ele) if ele.isdigit()
else ele, test_list))
# printing result
print("List after converting string numbers to integers : " + str(res))
输出 :
The original list : ['gfg', '1', 'is', '6', 'best']
List after converting string numbers to integers : ['gfg', 1, 'is', 6, 'best']