嗨,我正在转换字符串列表: lists=['111,222','121,121'] 在一个整数列表中,但不断出现错误,任何建议都会有帮助。 我试过:
lists=['111,222','121,121']
results=[int(i) for i in lists] print(results)
但是继续得到“int()的无效文本,基数为10:'111222'”
这应该管用
import re lists=['111,222','121,121'] results = [ int("".join(re.findall('[0-9]+', element))) for element in lists ] # results = [111222, 121121]
您需要删除逗号,例如:
lists=['111,222','121,121'] result = [int(s.replace(',', '')) for s in lists] print(result)
输出
[111222, 121121]