1

I'm trying to turn this into two lines (because of the global variable). I know that it's not the smartest or cleanest, but how can I make this into a two liner? I don't know how I could use two fors in one list comprehension.

def get_ngrams(n_gram=4):
    global text
    n_grams = []
    for word in text.split():
        n_grams += [word[i: i + 4]
                    for i in range(len(word)) if len(word) >= 4 and len(word[i: i + 4]) >= 4]
    return n_grams
rd360
  • 11
  • 2
  • 1
    Have you actually **tried** using two `for` clauses in a comprehension? (`[x*y for x in X for y in Y if y > x]`) – donkopotamus Mar 07 '20 at 20:11
  • 4
    how turning this into one comprehension would resolve your problem/use of global variable? Why not pass `text` as argument to function? And yes, it's possible to have 2 fors in one comprehension. – buran Mar 07 '20 at 20:11
  • 3
    You are concentrating on the wrong things. What you should be changing about this function is using the global variable, not trying to create an even more convoluted list comprehension that adds nothing but unreadability to your code. In any case, yes, a list comprehension can have as many for-clauses as it wants. – juanpa.arrivillaga Mar 07 '20 at 20:13
  • Does this answer your question? [How to frame two for loops in list comprehension python](https://stackoverflow.com/questions/18551458/how-to-frame-two-for-loops-in-list-comprehension-python) – mrzo Mar 07 '20 at 20:13
  • @juanpa.arrivillaga I know how to use a global variable, don't worry about that. – rd360 Mar 07 '20 at 20:24
  • @buran It's not a problem that I'm using a global variable. That's not even what I'm asking about, but thank you very much. – rd360 Mar 07 '20 at 20:24
  • @mrzo thank you! I think this may have solved my problem – rd360 Mar 07 '20 at 20:24
  • 2
    @rd360 yes, **it is a problem**. Relying on global state is a well-known anti-pattern. And apparently, you don't really know, since your `global` statement is totally unecessary – juanpa.arrivillaga Mar 07 '20 at 20:27
  • 1
    the way you phrased your question made me think you want to make it one comprehension "because of the global variable". Even if it was not the case - you should refactor your code and not use global variable. – buran Mar 07 '20 at 20:28
  • @juanpa.arrivillaga it's unnecessary with the context of the program you all have seen. this is a lab where text is the only form of text there IS throughout the lab. I can simply set it as global once because all I need to do is SOLVE for text. it stays the same. this isn't an actual program, and if it was, I wouldn't use a global variable. – rd360 Mar 07 '20 at 20:29
  • @buran that's fair. I should've rephrased my question. I just don't think it's necessary to not use a global variable when this is literally just for a lab in a class – rd360 Mar 07 '20 at 20:30
  • 2
    @rd360 no, it is unnecessary *in the context of this function* where your `global` statement does nothing useful. You could always use a the global `text` variable, you didn't need the `global` declaration. You must understand, this is a site for *programming questions*. It isn't a site for the best way for you to complete your lab. As a *programming practice* this would never pass any decent code review. But think whatever you want. – juanpa.arrivillaga Mar 07 '20 at 20:31
  • @juanpa.arrivillaga sure, so maybe it's unnecessary within the context of the function - why does that matter when that's not what I'm asking about? why not just actually answer the question? and yes, this is a site for programming questions - that's why i'm asking how to do a certain task. – rd360 Mar 07 '20 at 20:35
  • @rd360 because people are going to point out glaring issues with your code. If you don't want to take the advice, then other people who read this will. This site isn't just for you or me. In any case, your question has already been answered by being redirected to a duplicate. – juanpa.arrivillaga Mar 07 '20 at 20:37

0 Answers0