Advertisement
sreehariskumar

py-list of list

Mar 20th, 2023
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. python - list of list
  2. =====================
  3.  
  4. Questions
  5. ---------
  6. 1. list all words starts with "l" & "L"
  7.  
  8. for word in msg.split():
  9.  
  10. if word.startswith('l') or word.startswith('L'):
  11.  
  12. print(word)
  13.  
  14.  
  15. Result:
  16. -------
  17. lake
  18. like
  19. link
  20. Library.
  21. linux
  22. looking
  23. libraries
  24. low
  25. level
  26. Linux.
  27. LIKE
  28. link
  29. low
  30. level
  31.  
  32.  
  33. --------------------------------------------------------------------------------------------
  34.  
  35.  
  36. 2. add the words starts with "l" & "L" into a list called mywords( do not add duplicate entries ).
  37.  
  38.  
  39. mywords = []
  40.  
  41. for word in msg.split():
  42.  
  43. if word.startswith("l") or word.startswith("L"):
  44.  
  45. if word.lower().rstrip('.') not in mywords:
  46.  
  47. mywords.append(word)
  48.  
  49. print(mywords)
  50.  
  51.  
  52. Result:
  53. -------
  54. ['lake', 'like', 'link', 'Library.', 'linux', 'looking', 'libraries', 'low', 'level']
  55.  
  56.  
  57. --------------------------------------------------------------------------------------------
  58.  
  59.  
  60. 3 - print the total sum of each numbers.
  61.  
  62. numbers = [123 , 334 , 45 , 890 , 1234 , 45678 ]
  63.  
  64. for item in numbers:
  65.  
  66. numbers_str = str(item)
  67.  
  68. sum = 0
  69.  
  70. for num in numbers_str:
  71.  
  72. sum = sum + int(num)
  73.  
  74. print("sum of numbers : ",sum)
  75.  
  76.  
  77. Result:
  78. -------
  79. sum of numbers : 6
  80. sum of numbers : 10
  81. sum of numbers : 9
  82. sum of numbers : 17
  83. sum of numbers : 10
  84. sum of numbers : 30
  85.  
  86.  
  87. --------------------------------------------------------------------------------------------
  88.  
  89.  
  90. 4 - find the total sum of salaries.
  91.  
  92. employees = [ ["alex",50,35000] , ['sam',45,50000] , ["kevin",32,20000] ,[ "john",56,18000] ]
  93.  
  94. total_salary = 0
  95.  
  96. for item in employees:
  97.  
  98. salary = item[2]
  99.  
  100. total_salary = total_salary + salary
  101.  
  102. print(total_salary)
  103.  
  104.  
  105. Result:
  106. -------
  107. 123000
  108.  
  109.  
  110. --------------------------------------------------------------------------------------------
  111.  
  112.  
  113. 5 - create a list of list from string.
  114.  
  115. s = "Kernel component code executes"
  116.  
  117. result = []
  118.  
  119. for item in s.split():
  120.  
  121. length = len(item)
  122.  
  123. result.append( [item,length] )
  124.  
  125. print(result)
  126.  
  127.  
  128. Result:
  129. -------
  130. [['Kernel', 6], ['component', 9], ['code', 4], ['executes', 8]]
  131.  
  132.  
  133. --------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement