So uhhh what does my Coding Professor mean with this: 1. What is the output of: wordlist = ['cat','dog','rabbit'] letterlist = [ ] for aword in wordlist: for aletter in aword: letterlist.append(aletter) print(letterlist) Modify your code above so that only a single copy of each letter is output. I am confused as what he wants and don’t want to ask….
11/22/2021 1:42:12 AM
Caitlin Cook17 Answers
New AnswerCaitlin Cook Caitlin Cook I agree, it took me a bit, but I believe he just wants to know the output of the original code for #1. Which should be each letter from the words seperated in the form a list: [c, a, t, d, o...]
Caitlin Cook I have set out 3 snippets showing how to break it down. Uncomment to see the development words = ['cat','dog','rabbit'] letters= [ ] #for word in words: # print(word) #for word in words: # for letter in word: # print(letter) for word in words: for letter in word: if letter not in letters: letters.append(letter) print(letters)
My best guess is that he wants you to check to see if a letter in the letterlist was already appended. You can do this by implementing the following condition before appending a letter: if aletter not in letterlist: # append aletter to letterlist ...
wordlist = ['cat','dog','rabbit'] letterlist = [ ] for aword in wordlist: for aletter in aword: letterlist.append(aletter) print(letterlist) def fix(letterlist): s = set() list = [] for ch in letterlist: if ch not in s: s.add(ch) list.append(ch) return ''.join(list) print(fix(letterlist))
for aword in wordlist: for aletter in letterlist: letterlist.append(aletter) letterlist = set(letterlist) print(letterlist) A set contains only one copy of any element... So, casting as a set will do as well...
Hmmm so the code does that already but not as pretty it does [‘c’, ‘a’, ‘t’…] and on
I feel like he had a typo maybe? Cause the code already does what he is asking me to do…right?
Sololearn Inc.
535 Mission Street, Suite 1591Send us a message