- 1
Coding HW
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….
17 odpowiedzi
+ 3
Caitlin 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...]
+ 5
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)
+ 2
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
...
+ 2
Oooo he means no repeated letters
+ 1
Ipang oops typo thanks mate
+ 1
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))
+ 1
That't great, keep it
0
It works!!!! Thanks so much!!
0
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...
- 1
He is having me do that in the next problem so kinda confused on what he wants lol
- 1
Oooooo I get it now
- 1
Hmmm so the code does that already but not as pretty it does
[‘c’, ‘a’, ‘t’…] and on
- 1
I feel like he had a typo maybe? Cause the code already does what he is asking me to do…right?
- 1
Thanks everyone for help figuring that out!!!
- 1
Good
- 2
So like one letter from each word?