I'm trying to design a lambda function that returns any line in a file that begins with any digit [0-9]. The point would be to return the whole line if it does, and skip any line if it doesn't. This is for log analysis where I want all the lines that begin with an IP address.
I'm beginning to think this is too complex for a lambda expression. Any suggestions?
It shouldn't be too much for a lambda expression, so long as you recall a) that you can use a boolean expression such as in in a regular statement, and b) the string library includes a handy set of constant strings with the sets of letters, digits, and whitespace. All you should need to do is import string and apply in to your string's first character, and string.digits, and you should be able to use that as the lambda expression.
(OK, I'm sandbagging on this, as I've tested it and it works fine in Python 3.4, but I feel it is better to lead you to the code than simply give it to you. In this case, the explanation should be more than adequate for you to get the appropriate code worked out.)
Lambda expressions are not used so extensively and are kind of limitted in Python compared to functional programming like Lisp/Scheme. I would use simple basic expression, maybe list comprehension form:
[line for line in myfile if not line.isdigit()]
It is a good idea to strip() a record first, unless you are really really sure of the format.
Lambda is a bad habit IMHO. Guido wanted to omit lambda (and map, reduce, & filter) from Python3 because list comprehension is faster and the code is easier to understand. So form the habit of using list comprehension, or partial when you want to pass arguments to a function.