Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- python - list of list
- =====================
- Questions
- ---------
- 1. list all words starts with "l" & "L"
- for word in msg.split():
- if word.startswith('l') or word.startswith('L'):
- print(word)
- Result:
- -------
- lake
- like
- link
- Library.
- linux
- looking
- libraries
- low
- level
- Linux.
- LIKE
- link
- low
- level
- --------------------------------------------------------------------------------------------
- 2. add the words starts with "l" & "L" into a list called mywords( do not add duplicate entries ).
- mywords = []
- for word in msg.split():
- if word.startswith("l") or word.startswith("L"):
- if word.lower().rstrip('.') not in mywords:
- mywords.append(word)
- print(mywords)
- Result:
- -------
- ['lake', 'like', 'link', 'Library.', 'linux', 'looking', 'libraries', 'low', 'level']
- --------------------------------------------------------------------------------------------
- 3 - print the total sum of each numbers.
- numbers = [123 , 334 , 45 , 890 , 1234 , 45678 ]
- for item in numbers:
- numbers_str = str(item)
- sum = 0
- for num in numbers_str:
- sum = sum + int(num)
- print("sum of numbers : ",sum)
- Result:
- -------
- sum of numbers : 6
- sum of numbers : 10
- sum of numbers : 9
- sum of numbers : 17
- sum of numbers : 10
- sum of numbers : 30
- --------------------------------------------------------------------------------------------
- 4 - find the total sum of salaries.
- employees = [ ["alex",50,35000] , ['sam',45,50000] , ["kevin",32,20000] ,[ "john",56,18000] ]
- total_salary = 0
- for item in employees:
- salary = item[2]
- total_salary = total_salary + salary
- print(total_salary)
- Result:
- -------
- 123000
- --------------------------------------------------------------------------------------------
- 5 - create a list of list from string.
- s = "Kernel component code executes"
- result = []
- for item in s.split():
- length = len(item)
- result.append( [item,length] )
- print(result)
- Result:
- -------
- [['Kernel', 6], ['component', 9], ['code', 4], ['executes', 8]]
- --------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement