SHARE
TWEET

Untitled

a guest Nov 19th, 2019 88 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. somelist = [12, 45, 87,
  2.                 [65, 34, 76],
  3.                 [34, 76, 43,
  4.                     [8, 446, 23, 48,
  5.                         [23,56,43,787],
  6.                         {"key1": 543, "key2": 65, "key3": [54,2,335,76]}
  7.                     ],
  8.                     54, 23, 76, 32
  9.                 ],
  10.                 23, 65,
  11.                 {"key1": 54, "key2": 234, "key3": {"key1": 234, "key2": [1,245, 56, 45, (45,3,34,56)]}}
  12.                 ,23, 765, 22, "techacademy", "python", "coding", "bootcamp"
  13.            ]
  14.  
  15. def calc(item, sum=0):
  16.     if isinstance(item, tuple):
  17.         for it in item:
  18.             sum = calc(it, sum)
  19.         return sum
  20.     if isinstance(item, list):
  21.         for it in item:
  22.             sum = calc(it,sum)
  23.         return sum
  24.     elif isinstance(item, int):
  25.         return sum+item
  26.     elif isinstance(item, str):
  27.         return sum+len(item)
  28.     elif isinstance(item, dict):
  29.         for (k,v) in item.items():
  30.             sum = calc(v,sum)
  31.         return sum
  32.    
  33.  
  34. print(calc(somelist))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top