furas

example for stackoverflow

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