#this is all string
#a = ["a\n", "222\n", "bbb\n", "7777"]
#try with the below set
a = [10,11,'1','a','b',12,13,'14','c']
intPart = [x for x in a if isinstance(x, int)]
stringPart = [x for x in a if isinstance(x, str)]
print(intPart)
print(stringPart)
#output
#10, 11, 12, 13]
#['1', 'a', 'b', '14', 'c']
编辑:我不太明白OP所说的“把这个列表转换成int”是什么意思
我想你想要:
您可以尝试转换,如果不可能,则返回不变的值:
输出:
不知道你的期望是什么。但是我认为你的列表中有字符串,所以你得到了字符串。试试下面的方法,看看是否有用。你知道吗
相关问题 更多 >
编程相关推荐