Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Split into a list, then check the elements of the list
- with open(r"c:\Users\user\Desktop\warandpeace.txt", "r") as text:
- for line in text:
- line = [word.lower() for word in line.split(" ")]
- war += line.count("war")
- peace += line.count("peace")
- # split into a list, check the elements of the list using len and list comprehension
- with open(r"c:\Users\user\Desktop\warandpeace.txt", "r") as text:
- for line in text:
- line = [word.lower() for word in line.split(" ")]
- war += len([word for word in line if word == "war"])
- peace += len([word for word in line if word == "peace"])
- # words starting with the specified word
- with open(r"c:\Users\user\Desktop\warandpeace.txt", "r") as text:
- for line in text:
- words = [word.lower() for word in line.split(" ")]
- for word in words:
- if word.startswith("war"):
- war += 1
- if word.startswith("peace"):
- peace +=1
- # allow any substring to match
- with open(r"c:\Users\user\Desktop\warandpeace.txt", "r") as text:
- for line in text:
- war += line.count("war")
- peace += line.count("peace")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement