Python Lambda Count / Loop Function

Sorry if this question was answered elsewhere. Search through Google and Stackforum I did not find anything from which I could extrapolate the answers; but I feel that this is part of me.

I am trying to develop lambdas as a concept, and as part of this I was kind of looking for ways to use it.

SO, if this is a tremendously stupid deal with lambda in terms of function, feel free to let me know and explain. But anyway, 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 want to use lambda to create a single function that scrolls and returns the first process_name that is not in the test list. The functionality will eventually be applied to the file names, but for testing purposes I had to get away from actually reading the file names - there were still too many ways to spoil something.

But my_test should be able to change, and the list of tests will be a list of file paths.

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 the new name is in the test list. It seems that:

bool(new_name not in testlist)

maybe something works.

But I cannot figure out how to set the initial x to 1 and skip it with (x + 1) until the bool is true.

, , CRAZY lambda, . ( , .

? ( , test_name, test_name_dup, test_name_dup _ #)?

! ( ) .

+5
5

lambda, for :

my_test  = 'test_name_dup'  
testlist = ['test_name', 'test_name_dup','test_name_dup_1', 'test_name_dup_3']

for i in xrange(1, len(testlist)):
    if my_test + '_' + str(i) not in testlist:
        break

print my_test + '_' + str(i)
> test_name_dup_2

lambda , itertools, , .. thg435, :

import itertools as it

iterator = it.dropwhile(
    lambda n: '{0}_{1}'.format(my_test, n) in testlist,
    it.count(1))

print my_test + '_' + str(iterator.next())
> test_name_dup_2

dropwhile(). : , , ; .

iterable count(1), , , 1.

dropwhile() , ; - lambda. , , list_name_dup_ # .

false, dropwhile(), , , next() .

+2

Lambdas -

def foo(x):
    return x + x

foo = lambda x: x + x

, ,

def first_missing(items, base):
    for number in itertools.count():
        text = base + '_' + str(number)
        if text not in items:
             return text

, , , . .

def first_missing(items, base, number = 0):
        text = base + '_' + str(number)
        if text not in items:
             return text
        else:
             return first_missing(items, base, number + 1)

if/else .

def first_missing(items, base, number = 0):
        text = base + '_' + str(number)
        return text if text not in items else first_missing(items, base, number + 1)

, ,

def first_missing(items, base, number = 0):
        def inner(text = base + '_' + str(number)):
            return text if text not in items else first_missing(items, base, number + 1)
        return inner()

lambda

def first_missing(items, base, number = 0):
        inner = lambda text = base + '_' + str(number): text if text not in items else first_missing(items, base, number + 1)
        return inner()

,

def first_missing(items, base, number = 0):
    return (lambda text = base + '_' + str(number): text if text not in items else first_missing(items, base, number + 1))()

,

first_missing = lambda: items, base, number = 0: (lambda text = base + '_' + str(number): text if text not in items else first_missing(items, base, number + 1))()

, , . . , .

+5

itertools.dropwhile:

import itertools
n = itertools.dropwhile(lambda n: 'test_name_dup_%d' % n in testlist, range(1, len(testlist))).next()

, , :

def possible_names(prefix):
    yield prefix
    yield prefix + '_dup'
    n = 0
    while True:
        n += 1
        yield '%s_dup_%d' % (prefix, n)

dropwhile:

unique_name = itertools.dropwhile(lambda x: x in testlist, possible_names('test_name')).next()
print unique_name
+1

. Lambdas - , "" , . "", "", "", , itertools, , , , / ( ). , lambdas , / . , .

>>> filter(lambda i: i!=(0 if len(testlist[i].split("_"))==3 else int(testlist[i].split("_")[-1])), range(len(testlist)))[0]
2

itertools. , , . , .

[]

, lambdas + builtins , , : x (1000) x, 5.

$python -m timeit 'map (lambda x: x → 5, range (1000))' 1000 , 3: 225 usec

$python -m timeit '[x → 5 x (1000)]' 10000 , 3: 99.1 usec

> 100% lambdas.

+1

I prefer a list comprehension method or an iterator. It makes for light liners, which, it seems to me, are pretty easy to read and maintain. Honestly, lambdas belong to some places, here I think this is a less elegant solution.

my_test = 'test_name'
prefix = 'test_name_dup_'
testlist = ['test_name','test_name_dup','test_name_dup_1','test_name_dup_3']

from itertools import count
print next('%s%d' % (prefix, i) for i in count(1) if '%s%d' % (prefix, i) not in testlist)

This returns the first instance not found in the sequence, which I consider to be the cleanest.

Of course, if you prefer a list from a certain range, you can change it to become an understandable list:

print ['%s%d' % (prefix, i) for i in xrange(0,5) if '%s%d' % (prefix, i) not in testlist]

returns:

['test_name_dup_0', 'test_name_dup_2', 'test_name_dup_4']
+1
source

All Articles