SHARE
TWEET

example for stackoverflow

furas May 30th, 2020 976 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # date: 2020.05.30
  2. # https://stackoverflow.com/questions/62106026/how-to-use-string-variable-to-generate-name-of-the-lists-and-find-the-length-pyt
  3.  
  4. data = [
  5.     [0,1,9,10],
  6.     [2,3,11,12,13],
  7.     [4,5,6,7,8],
  8.     [16,17,25,26],
  9.     [14,15,22,23]
  10. ]
  11.  
  12. #-------------------------------------
  13.  
  14. nn = 0
  15. print('len', nn, ':', len(data[nn]) )
  16.  
  17. nn = 1
  18. print('len', nn, ':', len(data[nn]) )
  19.  
  20. #-------------------------------------
  21.  
  22. print('--- loop ---')
  23.  
  24. total = 0
  25. for item in data:
  26.     print('len:',  len(item) )
  27.     total += len(item)
  28.    
  29. print('total:', total)
  30.  
  31. #-------------------------------------
  32.  
  33. print('--- loop with enumerate ---')
  34.  
  35. total = 0
  36. for number, item in enumerate(data):
  37.     print('len', number, ':', len(item) )
  38.     total += len(item)
  39.    
  40. print('total:', total)
  41.  
  42. #-------------------------------------
  43.  
  44. print('--- list comprehension ---')
  45.  
  46. total = sum(len(item) for item in data)
  47. print('total:', total)
  48.  
  49. #-------------------------------------
  50.  
  51. print('--- single value ---')
  52.  
  53. print('value:', data[0][0])
  54. print('value:', data[0][1])
  55. print('value:', data[0][2])
  56. print('value:', data[0][3])
  57.  
  58. #-------------------------------------
  59.  
  60. print('--- single value in loop ---')
  61.  
  62. for value in data[0]:
  63.     print('value:', value)
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