Python Forum
how to write a code for term that can't begin or end with a stopword
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to write a code for term that can't begin or end with a stopword
#1
Hiiiiiiiiiiiiiiiiiiiii,

Need your small help for writing a code for selecting a term that can't begin or end with a stopword....

assume stopword list is there .....



is it possible to write using stratwith() and endwith()





Pray Pray Pray Pray Pray Pray Pray Pray
Reply
#2
You could use startswith() and endswith(), but that would probably be a bad idea. I'm assuming 'the' is one of your stop words. Using startswith('the') would trigger on 'theocracy is a form of government', and using endswith('the') would trigger on 'Dirty people need to bathe'.

You would do better to use split() to get a list of words, and then check if the first one (word_list[0]) or the last one (word_list[-1]) is in your list of stop words.

You may also want to use lower() so you can do a case insensitive search. Punctuation is another thing to deal with. That could be done either with translate() or a loop using replace().

So try some of those out, and come back with your code if you run into any problems. Reading the text at the top of the homework forum page would be helpful.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Punctuation could be removed with word.strip(string.punctualtion) from string module. It contains all those symbols: !"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~. Or just word.strip("'.,@?!-")
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#4
Or a regular expression:

import re

regex = re.compile(r"\W")
print(regex.sub("", "how do!you(do?"))
# howdoyoudo
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to write a dict code in python ariellea88 4 981 Oct-31-2023, 08:45 AM
Last Post: buran
  New learner in Python, and need help on the term explanation BaicaiPy 3 1,271 Oct-15-2022, 03:31 PM
Last Post: Yoriz
  how to write code demon_calcifer 1 1,613 Nov-16-2021, 04:09 PM
Last Post: Larz60+
  Can someone please help me write the code Ram 8 3,312 Feb-08-2021, 08:21 AM
Last Post: Serafim
  Please help me to write code for this vij 8 3,760 Jun-10-2020, 12:13 PM
Last Post: pyzyx3qwerty
  Write pseudo code for a class assignment Scrimshot 3 3,369 May-07-2019, 05:38 PM
Last Post: Scrimshot
  Filter out all words that begin with P, B, or T. johneven 3 2,704 May-07-2019, 02:54 PM
Last Post: michalmonday
  Write a code to output in alphabetical order AbdelaliPython 1 4,594 Jan-19-2018, 09:03 PM
Last Post: j.crater

Forum Jump:

User Panel Messages

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