Python Forum
For Word, Count in List (Counts.Items())
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
For Word, Count in List (Counts.Items())
#5
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:

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.
Reply


Messages In This Thread
RE: For Word, Count in List (Counts.Items()) - by new_coder_231013 - Jul-20-2022, 11:17 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Converting column of values into muliple columns of counts highland44 0 875 Feb-01-2024, 12:48 AM
Last Post: highland44
  Trying to get counts/sum/percentages from pandas similar to pivot table cubangt 6 3,227 Oct-06-2023, 04:32 PM
Last Post: cubangt
  Function to count words in a list up to and including Sam Oldman45 15 10,320 Sep-08-2023, 01:10 PM
Last Post: Pedroski55
  Why do I have to repeat items in list slices in order to make this work? Pythonica 7 2,828 May-22-2023, 10:39 PM
Last Post: ICanIBB
  Finding combinations of list of items (30 or so) LynnS 1 1,495 Jan-25-2023, 02:57 PM
Last Post: deanhystad
  Row Count and coloumn count Yegor123 4 2,631 Oct-18-2022, 03:52 AM
Last Post: Yegor123
  Read All Emails from Outlook and add the word counts to a DataFrame sanaman_2000 0 2,712 Sep-15-2022, 07:32 AM
Last Post: sanaman_2000
  find some word in text list file and a bit change to them RolanRoll 3 2,338 Jun-27-2022, 01:36 AM
Last Post: RolanRoll
  How to get list of exactly 10 items? Mark17 1 3,606 May-26-2022, 01:37 PM
Last Post: Mark17
  How to get unique entries in a list and the count of occurrence james2009 5 4,126 May-08-2022, 04:34 AM
Last Post: ndc85430

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020