I’m sorry if this is a question answered elsewhere. Searching through Google and Stackforum I didn’t find anything from which I could extrapolate the answers; but I feel like part of that is me.
I’m trying to work out lambdas as a concept, and as part of that I’m kinda looking for ways to use it.
SO, if this is a colossally stupid thing to do with lambda from a function standpoint, feel free to let me know and explain. But either way, I still want to know the answer/still want to know how to do this with the python language.
So, for testing purposes I have:
my_test = 'test_name'
testlist = ['test_name', 'test_name_dup', 'test_name_dup_1', 'test_name_dup_3']
I’m looking to use lambda to create one function that loops through and returns the first test_name_# that isn’t in the testlist. The functionality will eventually be applied to filenames, but for testing purposes I had to get away from actually reading the filenames–gave me too many more ways to mess something up.
But my_test has to be able to change, and the test list will be a list of filepaths.
So, I’m looking for a function like:
new_name = lambda x: my_test + '_' + str(x)
But the initial value should be x = 1, and it should continue until new_name is not in testlist. Seems like:
bool(new_name not in testlist)
might be something work with.
But I can’t figure out a way to set the initial x to 1, and have it loop through with (x+1) until the bool is true.
I know this is possible as I’ve found some CRAZY lambda examples out there that are looping through lines in a file. I just couldn’t quite make sense of them (and didn’t have any way to play with them as they were dealing with things outside my programming level.
On a related note, could I add values to the beginning of this loop? (i.e. can I have it check for test_name, then test_name_dup, then test_name_dup_#)?
Thanks in advance for the help! Lambdas (while very cool) totally mess with my head.
There’s no need to use a
lambdain this case, a simpleforloop will do:If you really, really want to use a
lambdafor this problem, you’ll also have to learn about itertools, iterators, filters, etc. I’m gonna build on thg435’s answer, writing it in a more idiomatic fashion and explaining it:The key to understanding the above solution lies in the
dropwhile()procedure. It takes two parameters: a predicate and an iterable, and returns an iterator that drops elements from the iterable as long as the predicate is true; afterwards, returns every element.For the iterable, I’m passing
count(1), an iterator that produces an infinite number of integers starting from1.Then
dropwhile()starts to consume the integers until the predicate is false; this is a good opportunity for passing an in-line defined function – and here’s ourlambda. It receives each generated integer in turn, checking to see if the string test_name_dup_# is present in the list.When the predicate returns
false,dropwhile()returns and we can retrieve the value that made it stop by callingnext()on it.