Explanation
Line 1:
array = [21, 49, 15] initializes the list.Line 2:
gen = (x for x in array if array.count(x) > 0) creates a generator:- It iterates over the current array ([21, 49, 15]).
- array.count(x) checks the count of each element in the array. Since all elements appear once (count > 0), they all qualify to be in the generator.
Line 3:
array = [0, 49, 88] reassigns array to a new list. However, this does not affect the generator because the generator already references the original array at the time of its creation.Line 4:
print(list(gen)) forces the generator to execute:- The generator still uses the original array = [21, 49, 15].
- The condition array.count(x) > 0 is true for all elements in the original list.
- Hence, the output is [21, 49, 15].
0 Comments:
Post a Comment