Monday, 2 January 2017

Python 3, How to remove part of an element of a list if it contains a specific thing3




I have a list of data that comes in the form of lists with numbers in them. as an intermediate step to making them all integers, I need to remove all the non-number parts within the element.




['jeff','69','420','80085\n']





how do I check to see if \n is in an element and how do I remove it if it is?




['jeff','69','420','80085']




Is the wanted output.
I literally don't know how to start, I can turn the numbers into integers using loops and intermediate variable but trying to identify things within them is not something i know how to do.



Answer



>>> input = ['jeff','69','420','80085\n']
>>> output = [x.replace('\n','') for x in input]
>>> output
['jeff', '69', '420', '80085']

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...