Answer the question
In order to leave comments, you need to log in
Generator or list?
Hello!
There is a task to calculate the sum of digits in a string. The number is passed as an argument. The question is this. As it is preferable to do - by converting a string of numbers into a generator or a list:
import sys
print(sum((int(x) for x in sys.argv[1]))) # генератор
или
print(sum([int(x) for x in sys.argv[1]])) # список
Answer the question
In order to leave comments, you need to log in
In almost any case, it is better to use generators, as they allow you not to store or calculate unnecessary data. Lists need to be formed only when one pass is not enough.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question