-->
当前位置:首页 > 题库

PROGRAMMING:Summation of numeric elements of a list or tuple

Luz5年前 (2021-05-10)题库388
Find the sum of numbers in the list, and the nesting level in the list is not limited to 2 levels
###Input format:
Enter a list or tuple on one line
###Output format:
Output the sum of the numbers in one line
###Input example:
Here is a set of inputs. For example:
```in
[11,2,[3,7],(68,-1),"123",9]
```
###Output example:
The corresponding output is given here. For example:
```out
ninety-nine
```







answer:If there is no answer, please comment
```
from collections import Iterable
def flatten(items,ignore_ type=(str,bytes)):
for x in items:
if isinstance(x,Iterable) and not isinstance(x,ignore_ type):
yield from flatten(x)
else:
if type(x)!= str:
yield x
#items=[1,2,[3,7],(68,-1),"123",9]
Items = Eval (input()) ා list input method
l=[i for i in flatten(items)]
print(sum(l))
```