Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
164 views
in Technique[技术] by (71.8m points)

python - Is there a way or workaround to insert "continue" statement in a list comprehension?

I wanna make a solution for this challenge on Hackerrank using python.

my code:

  • It's not valid syntax, I know but I want to make like this clue

    if __name__ == '__main__':
            x = int(1)
            y = int(1)
            z = int(1)
            n = int(2)
            result = [[i, j, k]
                      for i in range(n)
                      for j in range(n)
                      for k in range(n)
                      if n != sum([i, j, k]) else continue] # else continue, How to achieve?
            print(result)

Thanks in advance

question from:https://stackoverflow.com/questions/65884723/is-there-a-way-or-workaround-to-insert-continue-statement-in-a-list-comprehens

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

List comprehension with only if clause -

[i for i in l if condition]  #list only contains items that satisfy conditon

List comprehension with if and else -

[out1 if condition1 else out2 if i in l]  #list contains out1 where the condition is met and out2 where it is not

Your problem is of the first type. You are not using the right syntax for only the if clause.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...