QuestionQuestion

Transcribed TextTranscribed Text

Part 1: List Comprehensions A Python list comprehension allows you to create a list from another list/sequence. For example the list comprehensions below generate the output shown. Note that a list comprehension is a list defined by a for-loop inside square brackets, and that the loop iterates over some list, creating a new list in the process. In the examples below, the output of each print statement is shown in italics. somelist = [x*2 for x in range(0,10)] print somelist [0,2,4,6,8,10,12,14,16,18] somelist = [s[0] for s in ['apple', 'termite', 'elephant']] print somelist ['a','t','e'] somelist = [n % 2 == 0 for n in [2,4,6,1,3,5,8]] print somelist [True,True,True,False,False,False,True] somelist = [n for n in [2,4,6,1,3,5,8] if n % 2 == 0] print somelist [2,4,6,8] Without using a computer, (so by thinking) show what each line below prints. Enter your answers in the online form. print [x**2 for x in range(1,10,2)] print [s[1] for s in ["big", "brown", "bare", "stem", "pea"]] print ['po'*i for i in range(0,5)] print [i*i for i in range(1,10) if i % 2 == 0] print sum([1 for p in [1,2,3,4,5,6,7,8]]) print sum([1 for x in "ostentatiously" if "aeiou".find(x) > -1]) Generate List Comprehensions In writing your own list comprehensions it often helps to write intermediate expressions before creating the final list. Recall that the first variable in a comprehension determines the type of elements stored in the list. General steps in creating a list comprehension: - What type of element is stored in the list being created? - Is the list going to be a subset of the original list, or constructed using values from the original list, e.g., will it use x or len(x) for x in list - If a subset, or not the same length, then an if statement is likely needed Using a list comprehension write one expression using the function sum that returns the sum of all the odd integers in a list of integers named nums -- so for [1,3,2,4,5] your expression should evaluate to 9 --- fill in the parentheses: sum(…). Using a list comprehension and the list functions sum and len write an expression that returns the average length of all the strings in a list of strings named strs. So you'll call two list functions: one with strs as an argument and one with a list comprehension using strs as an argument: combining in one expression sum(...) and len(...). For example, the average length of the strings in the list below is (3+3+4+5)/4 = 15/4 = 3.75 strs = ["cat", "dog", "bear", “tiger"] Write a list comprehension that creates a list of indexes of those words in a list of strings named los that begin with the letter 't'. For example, los = ['the','turtle','was','a','tiny','green','troubled','animal'] The list comprehension should create [0,1,4,6], and it should work even when the values in los change to other strings. Part 2: AnagramFree Read the AnagramFree APT -- this part of the lab suggests using the Python construct set to make solving the APT simpler. In Python a set does not store duplicates, each value is only stored once. For example, the code below generates the output shown: x = set([1,2,3,1,2,3,1,2,3,1,1,1]) print len(x), x Output is: 3, set([1,2,3]) A set can be created from a list as shown, and elements can be added to the set using the set method .add, e.g., s = set() s.add("big") s.add("big") s.add("big") s.add("small") s.add("small") print len(s),s will generate 2, set([‘small','big']) Write an expression that determines the number of different strings in the list foods below, e.g., that sets diff to have the value 4, but will work even if the values in food change: foods = ["bagel", "bagel", "cheese", "bagel", "hummus" "onion", "bagel", "onion"] diff = For a string s, the value of sorted(s) is a sorted list of the individual characters in the string, e.g., sorted("apple") = ['a','e','l','p','p'] What's the value of the list comprehension below (write it out) --- you should have a list of lists: [sorted(w) for w in ['cow', 'dog', 'ant', 'bat']] The string method join creates a string from a list of strings as shown below. method call result ''.join(["a", "r", "t"]) "art" ' '.join(["the","big", "dog", "runs"]) "the big dog runs" The string to the left of the dot '.' is inserted between each string in the list that's a parameter to the join method and one string is returned as shown --- the new string is formed by concatenation. In the first call above an empty string '' is inserted, in the second a space ' ' is inserted. Using sorted and join write a list comprehension to create in unique the sorted string version of each string in words, e.g., to create ["aet", "aet", "aet", "abt", "abt", "deo" "deo"] out of the list ["eat", "aet", "tea", "bat", "tab", "ode", "doe"] words = ["eat", "aet", "tea", "bat", "tab", "ode", "doe"] unique = Using the techniques from the previous problems try to write a one line version of getMaximumSubset for the AnagramFree APT. Use set, sorted, join and a list comprehension. If you can't do it one line, use more than one. def getMaximumSubset(words): return len( )

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

Without using a computer, (so by thinking) show what each line below prints. Enter your answers in the online form.

print [x**2 for x in range(1,10,2)]
[1, 9, 25, 49, 81]

print [s[1] for s in ["big", "brown", "bare", "stem", "pea"]]
['i', 'r', 'a', 't', 'e']

print ['po'*i for i in range(0,5)]
['', 'po', 'popo', 'popopo', 'popopopo...

By purchasing this solution you'll be able to access the following files:
Solution.py and Solution.docx.

50% discount

Hours
Minutes
Seconds
$45.00 $22.50
for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Python Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats