Home > other >  For in the if converted into a for loop in general
For in the if converted into a for loop in general

Time:09-30

Today saw a add up to list any number is equal to the fixed value statement
The
reference
from itertools import chain, combinations
L=[1, 2, 3, 4, 5]
A=6
For c in print ([c chain (* (combinations (l, I) for I in range (1, len (l) + 1))) if the sum (c)==s])

Results: [(6), (1, 5), (2, 4), (1, 2, 3)]

This statement written in general for loop how ah, tried a few times all wrong...

CodePudding user response:

 
The from itertools import chain combinations
L=[6]
S=6

Cb=[]
For I in range (1, len (l) + 1) :
Cb. Append (combinations (l, I))

For c in chain (* cb) :
If the sum (c)==s:
Print (c)
  • Related