Task - Lists - List Comprehension #9
Replies: 10 comments 1 reply
-
1. Find all of the numbers from 1–1000 that are divisible by 8# divisable by 8 range(1,1000)
def main():
# using list comprehension
divisable8 = [num for num in range(1,1000+1) if num%8 == 0]
print(divisable8)
#run main
main() 2. Find all of the numbers from 1–1000 that have a 6 in them# all numbers with a 6
def main():
#list comprehension
digit6 = [num for num in range(1,1000+1) if '6' in str(num)]
print(digit6)
if __name__== "__main__":
main() 3. Count the number of spaces in a string (use string above)# count spaces
def main():
string = input("Enter some string: ")
spaces = [s for s in string if s == " "]
print(len(spaces))
if __name__ == "__main__":
main() 4. Remove all of the vowels in a string (use string above)# remove vowels
def main():
string = input("String with vowels: ")
noVowels = [s for s in string if s not in "aeiou"]
print(''.join(noVowels))
if __name__ == "__main__":
main() 5. Find all of the words in a string that are less than 5 letters (use string above)# words less than 5 letters
def main():
string = input("Some string with words")
shortWords = [word for word in string.split(" ") if len(word) < 5]
print(' '.join(shortWords))
if __name__ =="__main__":
main() 6. Use a dictionary comprehension to count the length of each word in a sentence (use string above)# word length in sentence using dictionary comprehension
def main():
sentence = input("Enter a sentence: ")
# delete punctiations
sentence = sentence.translate({ord(i): None for i in '.,;?!:'})
wordLen = {word: len(word) for word in sentence.split(" ")}
print(wordLen)
if __name__ == "__main__":
main() 7. Use a nested list comprehension to find all of the numbers from 1–1000 that are divisible by any single digit besides 1 (2–9)# find all numbers divisible by any digit besides 1 [2-9]
def main():
# nested list comprehension
# use set to only print unique numbers
divisibles = set([x for x in range(1,1000+1) for i in range(2,10) if x%i == 0 ])
print(list(divisibles))
if __name__ == "__main__":
main() 8. For all the numbers 1–1000, use a nested list/dictionary comprehension to find the highest single digit any of the numbers is divisible by# highest single digit divisor for each number
def main():
# nested list/dictionary comprehension
divisibles = {x:i for x in range(1,1000+1) for i in range(2,10) if x%i == 0}
# since a dictionary can not contain duplicate keys, and the digit divisor is in order,
# the highest single digit divisor will always prevails
print(divisibles)
if __name__ == "__main__":
main() |
Beta Was this translation helpful? Give feedback.
-
Task 1#Main Method
dividedby8 = [number for number in range(1, 1001) if number % 8 == 0]
print(dividedby8) Task 2#Main Method
listof6 = [number for number in range(1, 1001) if '6' in str(number)]
print(listof6) Task 3def chkSpaces(text):
count = 0
for i in range(0, len(text)):
if text[i] == " ":
count += 1
return count
#Main Method
test = input("Enter a text: ")
print("Number of spaces:", chkSpaces(test)) Task 4def remVowel(text):
#List with the vowels
vowels = ['a', 'e', 'i', 'o', 'u']
#Check if the letter is a vowel
result = [letter for letter in text if letter.lower() not in vowels]
result = ''.join(result)
return result
#Main Method
string = input("Enter a text: ")
print(remVowel(string)) Task 5def chkLess(text):
wordlist = [word for word in text if len(word) <= 4]
return wordlist
#Main Method
string = input("Enter a text: ")
print(chkLess(string.split())) Task 6def lenWords(text):
nwords = {word: len(word) for word in text.split(" ")}
return nwords
#Main Method
phrase = input("Enter a text: ")
print(lenWords(phrase)) Task 7#Main Method
nonPrimes = [num for num in range(1, 1001) if any(num % div == 0 for div in range(2, 10))]
print(nonPrimes) Task 8#Main Method
#This method collects all the dividers of one number, and then keeps the max value of each number.
results = [max([divisor for divisor in range(1,10) if number % divisor == 0]) for number in range(1,1001) ]
print(results) |
Beta Was this translation helpful? Give feedback.
-
Find all of the numbers from 1–1000 that are divisible by 8 divisible = [x for x in range(1,1001) if x%8==0]
print(divisible) Find all of the numbers from 1–1000 that have a 6 in them divisible = [x for x in range(1,1000+1) if '6' in str(x) ]
print(divisible) Count the number of spaces in a string (use string above) msg = input("Enter a message: ")
blankSpaces = [x for x in msg if ' ' in str(x)]
print(len(blankSpaces)) Remove all of the vowels in a string (use string above) msg = input("Enter a message: ")
vowels = ['a', 'e', 'i','o']
removeVowels = [x for x in msg if x not in vowels]
print(removeVowels) Find all of the words in a string that are less than 5 letters (use string above) msg = input("Enter a message: ")
msglen = [x for x in msg.split(" ") if len(x) < 5]
print(msglen) Use a dictionary comprehension to count the length of each word in a sentence (use string above) msg = input("Enter a message: ")
msglen = {x: x.__len__() for x in msg.split(" ")}
print(msglen) Use a nested list comprehension to find all of the numbers from 1–1000 that are divisible by any single digit besides 1 (2–9) divisible = [x for x in range(1,1001) for y in range(2,10) if x%y ==0]
print(divisible) For all the numbers 1–1000, use a nested list/dictionary comprehension to find the highest single digit any of the numbers is divisible by divisible = {x:y for x in range(1,1001) for y in range(1,10) if x%y ==0}
print(divisible) |
Beta Was this translation helpful? Give feedback.
-
list = [i for i in range(1, 1001) if i % 8 == 0]
print(list) list = [i for i in range(1, 1001) if "6" in str(i)]
print(list) st = input("Enter a string with spaces\n>")
list = [i for i in st if i == " "]
print(list.__len__()) st = input("Enter a string with vowels\n>")
vowels = ["a", "e", "i", "o", "u"]
list = [i for i in st if i not in vowels]
print(*list, sep="") st = input("Enter a string\n>")
words = st.split(" ")
list = [i for i in words if i.__len__() < 5]
print(list) st = input("Enter a sentence\n>")
list = {word: word.__len__() for word in st.split(" ")}
print(list) list = [number for number in range(1, 1001) for divisor in range(2, 10) if number % divisor == 0]
print(set(list)) list = {number: divisor for number in range(1, 1001) for divisor in range(1, 10) if number % divisor == 0}
print(list) |
Beta Was this translation helpful? Give feedback.
-
def getNum():
divisors = [num for num in range(1,1001) if num%8 ==0]
print(divisors)
getNum() def getNum():
numList = [num for num in range(1,1001) if '6' in str(num)]
print(numList)
getNum() text = "hello i think that the seahawks are going to lose tonight"
spaces = [space for space in text if space == " "]
print(len(spaces)) text = "hello i think that the seahawks are going to lose tonight"
textList = text.split()
vowels = ['a', 'e', 'i', 'o', 'u']
newList = [letter for letter in text if letter not in vowels]
newList = ''.join(newList)
print(newList) text = "hello i think that the seahawks are going to lose tonight"
textList = text.split()
newList=[x for x in textList if len(x) < 5]
print(newList) text = "hello i think that the seahawks are going to lose tonight"
textList = text.split()
textDic = {}
for word in textList:
textDic[word] = len(word)
print(textDic) num = set([i for i in range(1,1001) for k in range(2,10) if i%k==0])
print(num) num = {number:divisor for number in range(1,1001) for divisor in range(1,10) if number%divisor==0}
print(num) |
Beta Was this translation helpful? Give feedback.
-
div = [num for num in range(1,1001) if num % 8 == 0]
print(div)
#
x = [num for num in range(1,10001) if '6' in str(num)]
print(x)
#
s = "welcome to python world"
y = [x for x in range(0,len(s)) if s[x] == " "]
z = len(y)
print(z)
#
word="welcome to python world"
w = [j for j in range(0,len(word)) if j in "aeiou"]
wo = word.replace(j,"")
print(wo) |
Beta Was this translation helpful? Give feedback.
-
def main():
bigin =1
end =1000
divisable8 = [num for num in range(1,1000+1) if num%8 == 0]
print(divisable8)
main() X= [num for num in range(1,1000+1) if '6' in str(num)]
print(X) stg = input("To is a good day")
X = [space for space in len(stg) (space=" ")]
print(X) text = input("Chriss is playing football")
vowels =("a","e","i","o","u")
stg = [X for X in text if X not in vowels]
print(stg) string = "Welcome to python program"
text = string.split()
list = [x for x in string if len(x) < 5]
print(list) def count_words_by_length_compr1(words):
return {
len(word): sum(1 for word_ in words if len(word_) == len(word))
for word in words}
mod_text = ["hello", "this", "is", "a", "list", "of","words"]
print(count_words_by_length_compr1(mod_text)) X = [number for number in range(1, 1001) for divisor in range(2, 10) if number % divisor == 0]
print(set(X)) divisible = {i:j for i in range(1,1001) for j in range(1,10) if i%j ==0}
print(divisible) |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
1.- def divisibleby8(num):
numbers = []
for n in range(1, num):
if n % 8 == 0:
numbers.append(n)
print(numbers)
def main():
divisibleby8(1000)
main() 2.- def main():
num6 = []
for num in range(1, 1000 + 1):
if '6' in str(num):
num6.append(num)
print(num6)
main() 3.- def main():
phrase = input("enter a text:")
space = 0
for i in range(len(phrase)):
if phrase[i:i+1] == " ":
space = space + 1
print(space)
main() 4.- def main():
vowels = ("a","e","i","o","u")
phrase = input("enter a text:")
words = [i for i in phrase if i not in vowels]
newphrase = ""
for i in range(len(words)):
newphrase = newphrase + words[i]
print(newphrase)
main() 5.- def main():
phrase = input("enter a text:")
words = phrase.split(" ")
words5 = [word for word in words if len(word) <= 5]
print(words5)
main() 6.- def main():
phrase = input("enter a text:")
lenwords ={word: len(word) for word in phrase.split(" ")}
print(lenwords)
main() 7.- def main():
numbers = [i for i in range(1, 1001) for y in range(2, 10) if i % y == 0]
print(list(dict.fromkeys(numbers)))
main() 8.- def main():
Numberdivisor = {n: d for n in range(1, 1001) for d in range(1, 10) if n % d == 0}
print(Numberdivisor)
main() |
Beta Was this translation helpful? Give feedback.
-
1.- Find all of the numbers from 1–1000 that are divisible by 8 def main():
divisible_by_eight = []
for i in range(1, 1001):
if i % 8 == 0:
divisible_by_eight.append(i)
print(divisible_by_eight)
if __name__ == '__main__':
main() 2.- Find all of the numbers from 1–1000 that have a 6 in them. def main():
have_six = [x for x in range(1, 1001) if '6' in str(x)]
print(have_six)
if __name__ == '__main__':
main() 3.- Count the number of spaces in a string (use string above) def main():
text = input("Enter a string: \n")
text = text.strip()
number_of_spaces = text.count(' ')
print(number_of_spaces)
if __name__ == '__main__':
main() 4.- Remove all of the vowels in a string (use string above) def main():
text = input("Enter a string: \n")
text = text.strip()
vowels = [v for v in text.lower() if v not in 'aeiou']
print(''.join(vowels))
if __name__ == '__main__':
main() 5.- Find all of the words in a string that are less than 5 letters (use string above) def main():
text = input("Enter a string: \n")
text = text.strip()
text = text.split(" ")
less_five = [w for w in text if len(w) < 5]
print(less_five)
if __name__ == '__main__':
main() 6.- Use a dictionary comprehension to count the length of each word in a sentence (use string above) def main():
text = input("Enter a sentence: ")
text = text.strip()
count_word = {w : len(w) for w in text.split(" ")}
print(count_word)
if __name__ == '__main__':
main() 7.- Use a nested list comprehension to find all of the numbers from 1–1000 that are divisible by any single digit besides 1 (2–9) def main():
div_numbers = set([x for x in range(1, 1001) for i in range(2, 10) if x % i == 0])
print(list(div_numbers))
if __name__ == '__main__':
main() 8.- For all the numbers 1–1000, use a nested list/dictionary comprehension to find the highest single digit any of the numbers is divisible by def main():
highest_div = {x : d for x in range(1,1001) for d in range(1, 10) if x % d == 0}
print(highest_div)
if __name__ == '__main__':
main() |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Find all of the numbers from 1–1000 that are divisible by 8
Find all of the numbers from 1–1000 that have a 6 in them
Count the number of spaces in a string (use string above)
Remove all of the vowels in a string (use string above)
Find all of the words in a string that are less than 5 letters (use string above)
Use a dictionary comprehension to count the length of each word in a sentence (use string above)
Use a nested list comprehension to find all of the numbers from 1–1000 that are divisible by any single digit besides 1 (2–9)
For all the numbers 1–1000, use a nested list/dictionary comprehension to find the highest single digit any of the numbers is divisible by
Beta Was this translation helpful? Give feedback.
All reactions