Passing a second input to itertools deopwhile

asked 2023-04-11 15:16:01 +0100

Cyrille gravatar image

This works

def doesnt_contain_character(str,sub):
    substring = sub
    if substring in str:
        return False
    else:
        return True

doesnt_contain_character('ipsum','a')

but this

string_list = ['lorem', 'ipsum', 'dolor', 'sit', 'amet']
print(list(it.dropwhile(doesnt_contain_character(str,'a'), string_list)))

not because I need to pass 'a'. In brief all exemple of dropwhile on the net doesn't need two inputs. Is there a way to do that ?

edit retag flag offensive close merge delete

Comments

Why do you insist to use the parameter str - thus overwriting one of the main types in python, then do something exactly with this type? Well, typing against pep8 is somehow tolerated on this site, but using that str makes reading the whole post irritating.

dan_fulea gravatar imagedan_fulea ( 2023-04-11 16:02:29 +0100 )edit

Please always provide working code. I guess that it is a shorthand for itertools - but please make our life easier. Please always explain what the code should do, how it is related to some sagemath functionality, and which is the unexpected error / behavior. If there is a "brief example on the net" - please link it. All in all, a question should come as digested as possible, so that potential answerers can go straightforward to the answer - and so that potential readers, now or after the years, can also profit from the question and the answer. Making a question searchable is also a good point. If some functionality inside itertools is the main issue, then mention it in a clear manner. What is "second input"? This question is downvoted because of repeated and repeated way doing so.

dan_fulea gravatar imagedan_fulea ( 2023-04-11 16:03:52 +0100 )edit

How this question is relevant to Sage?

Max Alekseyev gravatar imageMax Alekseyev ( 2023-04-12 00:06:24 +0100 )edit

I second the comments already made. I note (with Max) that this is, again, a Python question (and, more fundamentally, a "basic programming" question)...

Furthermore, I have trouble making head and tail of what you want to accomplish. Could you give a couple of examples ? And in what context you have a "second parameter" problem ?

Remember that you can always enclose ("nest" a function as local to another one, which will find the outer function's parameters "naturally".

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2023-04-13 20:13:19 +0100 )edit