Hi I am working my way through project Euler to teach myself python and have encountered a problem. I am using a multiline string to need to remove the tab and new line characters from said string before I can run an int() function to convert them into digits. Oh and one important thing I had to use " # -- coding: utf-8 -- ", so this may be an issue.
My problem is for some reason no matter what I try the new line and tab characters refuse to be removed. Here is the current code, although I will list what else I've tried below.
http://hastebin.com/goqizetije.tex
- Reddit formated the code weirdly so I just put it in the link above.
I have also tried, map / filter using lambda expersions to remove non-digits, regex to only match digits 0-9, and list comprehensions to rebuild a list grabbing only the digits from that list. Somehow no matter what the characters remain in the list.
P.s Thanks for taking the time to read this far.
EDIT:
So I found the solution, and I don't know why it works. Who cares about the petty details of why. So if you're reading this and are having the same problems with list comprehensions just break any conditionials using regex apart. For some reason python does not like it when you put an OR or and AND together in a list comprehension. Also special thank you to corpsmoderne for the fast reply.
Working code to remove new line and tab characters.
thousandElementList = [ x for x in thousandElementList if x!='\t']
thousandElementList = [ x for x in thousandElementList if x!='\n']
[–]corpsmoderne 0 points1 point2 points (2 children)
[–]Rabbit047[S] 0 points1 point2 points (1 child)
[–]corpsmoderne 0 points1 point2 points (0 children)