列表元素有字母有数字怎样将数字转数字
可以使用Python内置的isdigit()函数判断一个字符串是否只包含数字,如果是则使用int()函数将其转换为数字类型。具体代码如下:
my_list = ['a', 'b', '1', '2', 'c', '3']
new_list = []
for item in my_list:
if item.isdigit():
new_list.append(int(item))
else:
new_list.append(item)
print(new_list)
运行结果为:['a', 'b', 1, 2, 'c', 3]。
AI 优化版
原文地址:https://www.cveoy.top/t/topic/bjUU 著作权归作者所有。请勿转载和采集!