hi I'm trying to convert this list of strings: lists=['111,222','121,121']
into a list of integers but keep running into errors, any advice would be helpful. I've tried:
results=[int(i) for i in lists]
print(results)
but keep getting "invalid literal for int() with base 10: '111,222'"
You need to remove the commas, for example:
lists=['111,222','121,121']
result = [int(s.replace(',', '')) for s in lists]
print(result)
Output
[111222, 121121]
This should work
import re
lists=['111,222','121,121']
results = [ int("".join(re.findall('[0-9]+', element))) for element in lists ]
# results = [111222, 121121]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.