3

I'm new to python and I'm trying to teach myself how to use it by completing tasks. I am trying to complete the task below and have written the code beneath it. However, my code does not disregard the punctuation of the input sentence and does not store the sentence's words in a list. What do I need to add to it? (keep in mind, I am BRAND NEW to python, so I have very little knowledge)

Develop a program that identifies individual words in a sentence, stores these in a list and replaces each word in the original sentence with the position of that word in the list.

For example, the sentence:

ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU ASK WHAT YOU CAN DO FOR YOUR COUNTRY

contains the words ASK, NOT, WHAT, YOUR, COUNTRY, CAN, DO, FOR, YOU

The sentence can be recreated from the positions of these words in this list using the sequence

1,2,3,4,5,6,7,8,9,1,3,9,6,7,8,4,5

Save the list of words and the positions of these words in the sentence as separate files or as a single file.

Analyse the requirements for this system and design, develop, test and evaluate a program to:

• identify the individual words in a sentence and store them in a list

• create a list of positions for words in that list

• save these lists as a single file or as separate files.

restart = 'y'
while (True):
    sentence = input("What is your sentence?: ")
    sentence_split = sentence.split()
    sentence2 = [0]
    print(sentence)
    for count, i in enumerate(sentence_split):
        if sentence_split.count(i) < 2:
            sentence2.append(max(sentence2) + 1)
        else:
            sentence2.append(sentence_split.index(i) +1)
    sentence2.remove(0)
    print(sentence2)
    restart = input("would you like restart the programme y/n?").lower()
    if (restart == "n"):
            print ("programme terminated")
            break
    elif (restart == "y"):
        pass
    else:
        print ("Please enter y or n")
tripleee
  • 139,311
  • 24
  • 207
  • 268

2 Answers2

0

Since this are several question in one, here's a few pointers (I won't help you with the file I/O as that's not really part of the problem).

First, to filter punctuation from a sentence, refer to this question.

Second, in order to get an ordered list of unique words and their first positions, you can use an ordered dictionary. Demonstration:

>>> from collections import OrderedDict
>>> s = 'ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU ASK WHAT YOU CAN DO FOR YOUR COUNTRY'
>>> words = s.split()
>>> word2pos = OrderedDict()
>>> 
>>> for index, word in enumerate(words, 1):
...     if word not in word2pos:
...         word2pos[word] = index
... 
>>> word2pos.keys()
['ASK', 'NOT', 'WHAT', 'YOUR', 'COUNTRY', 'CAN', 'DO', 'FOR', 'YOU']

If you are not allowed to use an ordered dictionary you will have to work a little harder and read through the answers of this question.

Finally, once you have a mapping of words to their first position, no matter how you acquired it, creating the list of positions is straight forward:

>>> [word2pos[word] for word in words]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 3, 9, 6, 7, 8, 4, 5]
Community
  • 1
  • 1
timgeb
  • 64,821
  • 18
  • 95
  • 124
0

You have to consider a few things before, such as what do you do with punctuation as you already noted. Now, considering that you are trying to teach yourself I will attempt to only give you some tips and information that you can look at.

The [strip] command can allow you to remove certain letters/numbers from a sentence, such as a , or ..

The split command will split a string into a list of smaller strings, based on your splitting command. However, to see the place they had in the original string you could look at the index of the list. For instance, in your sentence list you can get the first word by accessing sentence[0] and so forth.

However, considering that words can be repeated this will be a bit trickier, so you might look into something called a dictionary, which is perfect for what you want to do as it allows you do something as follows:

words = {'Word': 'Ask', 'Position': [1,10]}

Now if you stuck with the simplistic approach (using a list), you can iterate over the list with an index and process each word inidividually to write them to a file, for instance along the lines of (warning, this is pseudo code).

for index, word in sentence:
    do things with word
    write things to a file

To get a more 'true' starting point check the below spoiler

for index, word in enumerate(sentence): filename = str(word)+".txt" with open(filename,'w') as fw: fw.write("Word: "+str(word)+"\tPlace: "+str(index))

I hope this gets you under way!

Bas Jansen
  • 3,010
  • 4
  • 26
  • 60