Python Forum
How to find unique and common words per line from a txt file?
Thread Rating:
  • 2 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to find unique and common words per line from a txt file?
#7
You can make this a little shorter
odds, even = eo = [set(), set()]
with open('shakespeare.txt') as f:
    for index, row in enumerate(f):
        eo[index % 2].update(row.strip().split())
Observe that python numbers lines from 0 while common CS and programming literature and software starts line counts at 1.
Reply


Messages In This Thread
RE: How to find unique and common words per line from a txt file? - by Gribouillis - Aug-11-2018, 01:28 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  find and group similar words with re? cartonics 4 877 Oct-27-2023, 05:36 PM
Last Post: deanhystad
  FileNotFoundError: [WinError 2] The system cannot find the file specified NewBiee 2 1,769 Jul-31-2023, 11:42 AM
Last Post: deanhystad
  File "<string>", line 19, in <module> error is related to what? Frankduc 9 12,908 Mar-09-2023, 07:22 AM
Last Post: LocklearSusan
  Cannot find py credentials file standenman 5 1,796 Feb-25-2023, 08:30 PM
Last Post: Jeff900
  selenium can't find a file in my desk ? SouAmego22 0 809 Feb-14-2023, 03:21 PM
Last Post: SouAmego22
  Getting last line of each line occurrence in a file tester_V 1 970 Jan-31-2023, 09:29 PM
Last Post: deanhystad
  Need to match two words in a line tester_V 2 945 Nov-18-2022, 03:13 AM
Last Post: tester_V
  Find (each) element from a list in a file tester_V 3 1,348 Nov-15-2022, 08:40 PM
Last Post: tester_V
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 1,555 Sep-27-2022, 01:38 PM
Last Post: buran
  what will be the best way to find data in txt file? korenron 2 1,247 Jul-25-2022, 10:03 AM
Last Post: korenron

Forum Jump:

User Panel Messages

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