I had to divide up the four letter words based upon the last two letters of the word (the word ending) and Count up how many words you have for each of these endings. I was able to do this part but in the next part PROBLEM:
I need to only work with word endings where there are at least 30 words. And print out a list of word endings and the number of words you found for each ending, which I have not been able to do because the way I'm doing it gives me a list of the words with the endings and not in the format that i need which is: as:30 as:35
I need to Randomly choose one word from those available for each of the word endings to use in your stimuli and print a list of all possible words and count them
endings_dict = {}
for word in unique_words:
endings_dict[word[2:]] = endings_dict.get(word[2:], []) [word]
print(endings_dict)
print({key: len(value) for key, value in endings_dict.items()})
for v in endings_dict.values(): #this part doesn't work
if len(v) >= 30:
print(v)
UPDATE: I was able to correct the code and I believe I also manages to get the random part
for key, value in endings_dict.items():
if len(value) >= 30:
print(key, len(value))
import random
for v in endings_dict.values():
if len(v) >= 30:
random.choice(v)
print(random.choice(v))
print(len(random.choice(v)))
BUT the len part it still not working because it is not giving me the number of all the random words hat were generated
CodePudding user response:
Just made a small change to your "This Dosent Work" section
We are looking for the exact same outcome as you have here
print({key: len(value) for key, value in endings_dict.items()})
but we just want to put a condition on it.
To do so we will use the following. Do understand what is happening it might be useful for you to add a print statement to here to see what item[0]
and item[1]
are actually comprised of
for item in endings_dict.items():
if len(item[1]) >= threshold:
print({item[0]:len(item[1])})
This allows us to output in the proper format and apply our condition
the full code with the list i used is as follows
unique_words = ["peep","keep","mope","hope","blow","love","beat","meat","plea","flow","brow","mile","file","nile","bile","tile"]
endings_dict = {}
for word in unique_words:
endings_dict[word[2:]] = endings_dict.get(word[2:], []) [word]
print(endings_dict)
print({key: len(value) for key, value in endings_dict.items()})
threshold = 4
for k,v in endings_dict.items():
if len(v) >= threshold:
print({k:len(v)})
EDIT:
Re read the question again and saw i missed out on part 2:
for k,v in endings_dict.items():
if len(v)>= threshold:
endings_pass_dict[k]=v
endings_pass_formated_dict[k]=len(v)
randomIndex = random.randint(0,len(endings_pass_dict.items())-1)
k = list(endings_pass_dict.keys())
v = list(endings_pass_dict.values())
print(k[randomIndex],v[randomIndex],len(v[randomIndex]))