Jul-20-2022, 11:17 AM
Thanks snippsat! I've looked over what you wrote and it actually generated some more questions.
1 ) I’m curious why the code from the introductory Python book ignores punctuation. Does it have something to do with the way the split() method works? Or possibly the get function (which appears two lines down from the split() method in the code)?
2 ) You provided three separate sets of code. Does the first set of code you provided need to go with the second set of code in order for the second set of code to work? I think the answer is "No" but I'm not sure.
3 ) On line 8 in the second set of code, doesn’t the for loop need to read “for word in words” (since word hasn’t been previously defined and Python needs to know which part of the code to iterate through)?
Also on lines 8 and 9, where is count coming from or what is it doing? Is this the count() method (used for lists)? Or is it what was already defined as “count = Counter(1st)” in the previous set of code? Which method or function is "count" here?
I’m trying to figure out what is actually going on in the print statement on the last line of the second set of code you wrote. At first I thought that it would only print those sets of words that have 1) A “value” of less than 8 (that value being defined as the number of characters in the string) and 2) That appear less than five times in the text. But I modified the code slightly to experiment with this and it returned words that violated these conditions:
Thanks again for your help and have a great day.
1 ) I’m curious why the code from the introductory Python book ignores punctuation. Does it have something to do with the way the split() method works? Or possibly the get function (which appears two lines down from the split() method in the code)?
2 ) You provided three separate sets of code. Does the first set of code you provided need to go with the second set of code in order for the second set of code to work? I think the answer is "No" but I'm not sure.
3 ) On line 8 in the second set of code, doesn’t the for loop need to read “for word in words” (since word hasn’t been previously defined and Python needs to know which part of the code to iterate through)?
Also on lines 8 and 9, where is count coming from or what is it doing? Is this the count() method (used for lists)? Or is it what was already defined as “count = Counter(1st)” in the previous set of code? Which method or function is "count" here?
I’m trying to figure out what is actually going on in the print statement on the last line of the second set of code you wrote. At first I thought that it would only print those sets of words that have 1) A “value” of less than 8 (that value being defined as the number of characters in the string) and 2) That appear less than five times in the text. But I modified the code slightly to experiment with this and it returned words that violated these conditions:
from collections import Counter import re text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse porta neque nulla, condimentum ultrices tortor vehicula gravida. Suspendisse a lacinia nunc. Etiam lobortis nunc ipsum, ac malesuada erat fermentum id. Sed arcu neque, fringilla ut mauris sit amet, vehicula auctor augue. Pellentesque ut mi erat. Donec consequat eleifend aliquam. Proin" readtext = text.lower() words = re.findall ('\w+', readtext) top_10 = Counter(words).most_common(10) for word, count in top_10: print(f'{word:<8} --> {count:>5}')
Output:ipsum --> 2
sit --> 2
amet --> 2
suspendisse --> 2
neque --> 2
vehicula --> 2
nunc --> 2
erat --> 2
ut --> 2
lorem --> 1
I’m also unclear on what is going on with “count” in the for loop (I’m familiar with using “in” to check whether something appears in something else but I’m not sure how “count” – which hasn’t previously been defined and which I’m not sure which function or method it is – would play into that).Thanks again for your help and have a great day.