how do I write the positions to a file - python

The full task that I have been assigned from school is:
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.
Here is my current code:
sentencelist=[] #variable list for the sentences
word=[] #variable list for the words
positionofword=[]
words= open("words.txt","w")
position= open("position.txt","w")
question=input("Do you want to enter a sentence? Answers are Y or N.").upper()
if question=="Y":
sentence=input("Please enter a sentance").upper() #sets to uppercase so it's easier to read
sentencetext=sentence.isalpha or sentence.isspace()
while sentencetext==False: #if letters have not been entered
print("Only letters are allowed") #error message
sentence=input("Please enter a sentence").upper() #asks the question again
sentencetext=sentence.isalpha #checks if letters have been entered this time
elif question=="N":
print("The program will now close")
else:
print("please enter a letter")
sentence_word = sentence.split(' ')
for (i, check) in enumerate(word): #orders the words
print(sentence)
sentence_words = sentence.split(' ')
for (i, check) in enumerate(sentence_words): #orders the words
if (check == word):
positionofwords=print(i+1)
break
else:
print("This didn't work")
words.write(str(sentence_words) + " ")
position.write(str(positionofwords) + " ")
words.close()
position.close()
This doesn't work. The error I get is:
NameError: name 'positionofwords' is not defined
What I would like to know is why positionofwords=print(i+1) does not work in this case and what I would do instead.

>>> s = 'ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU ASK WHAT YOU CAN DO FOR YOUR COUNTRY'
>>> words = s.split()
>>> for w in words:
print(words.index(w) + 1)
Output:
1
2
3
4
5
6
7
8
9
1
3
9
6
7
8
4
5

Related

How to determine the number of times a word appears in an input

I have a situation, the user can input a list of words separated by "," (For example: iceCream, Dog, Cat). At the end, I have to say how many words the user inputted, how many letters he entered and the number of times the user inputs the word "yeet" ( with two or more "e"). Example, if the user inputs: dog,cat,yeet,yeeet,yeete. The program will return "you used 5 words, 20 letters and 2 yeets". This is what I have so far:
array = input("Enter your words seperated by a comma,(for e.g rohit,bohit,sohit,lohit)").lower()
words = len(array.split(","))
letters = len(array)
yeets = 0
for i in array:
if array.startswith("y") and array.endswith("t"):
yeets += 1
print("You entered", words, "words with", letters, "letters and it has", yeets, "yeets!")
In your for loop, use a regex that it starts with y, ends with t, and has 2 or more e's in the middle
import re
for word in array:
if re.match("^yee+t$", word):
yeets += 1
EDIT: uses regex instead of perfect match
You need to loop over the words in the string, not the characters in the string.
You can use a regular expression to match y followed by at least 2 e followed by t.
import re
wordlist = array.split(',')
yeets = len([word for word in wordlist if re.match(r'^ye{2,}t$', word)])

Count Non-Substring Overlapping

Write a program allows user to input a string. End the program is printing out:
a. How many words that repeated itself.
For example: 'This is Jake and Jake is 24 years old'
The console must print out '4' because 'is' and 'Jake' are the word that repeated
b. Remove all the repeated word. Print out the rest: 'This and 24 years old'
c. Print out which repeated words have been removed
So the idea is the user can type whatever they want, 'This is Jake and Jake is 24 years old' is just an example. The hardest part is how can console check all the repeated words without a substring?
Does this work?
Here is what I am doing.
First I grab the user input, then I convert the string to a list splitting on spaces between words. Then I count the occurence of the words, if the wordcount is greater than 1, I add it to a dictionary , where the key is the word and the value is the count of the words that exist in the string.
After printing out the repeated words, I remove the strings that find a mention in the dictionary.
Note - This code can be improved so much but I purposely did it such a manner to make it easier to understand. You should not be using this code if its a production system.
string = input("Enter your string : ")
items = {}
words = string.split(" ")
for word in words:
wordCount = words.count(word)
if(wordCount > 1):
items[word] = wordCount
print("There are {0} repeated words".format(len(items)))
updateString = ""
for item in items:
updateString =string.replace(item,"")
print(updateString)
print(items)
Updated
string = input("Enter your string : ")
items = {}
words = string.split(" ")
for word in words:
wordCount = words.count(word)
if(wordCount > 1):
items[word] = wordCount
print("There are {0} repeated words".format(len(items)))
for item in items:
string = string.replace(" {0} ".format(item)," ")
print(string)
print(items)

I need to make it so the sentence and numbers that are outputted are stored into a file

The code I have made:
asks the user for a sentence,
makes it lower case so it is not case sensitive
splits the sentence into separate words so each word can have a number assigned to it according to its position
How can I add a part to my code that saves the sentence inputted by
the user as a file, along with the numbers that get assigned to each
word?
Here is my code:
sentence = input("Please enter a sentence")
sentence = sentence.lower()
sentence = sentence.split()
positions = [sentence.index (x) +1 for x in sentence]
print(sentence)
print(positions)
Use raw_input if you want to treat everything as strings. You do not need to store the positions but, instead, get them from the wonderful enumerate function. Then write to the file like so
sentence = raw_input("Please enter a sentence: ")
sentence = sentence.lower()
sentence = sentence.split()
open('filename.txt','w').writelines(["%d-%s\n"%(i+1,x) for (i,x) in enumerate(sentence)])
sentence = input("Please enter a sentence")
sentence = sentence.lower()
sentence = sentence.split()
wordPositionDict = {}
( wordPositionDict.get(x,[]).append(i+1) for i,x in enumerate(sentence))
print wordPositionDict[word]
append all the index for each word to dict. after iterate all the words in sentence you will have one dict which key is the words and value is the list of index

Replacing and Storing

So, here is what I got:
def getSentence():
sentence = input("What is your sentence? ").upper()
if sentence == "":
print("You haven't entered a sentence. Please re-enter a sentence.")
getSentence()
elif sentence.isdigit():
print("You have entered numbers. Please re-enter a sentence.")
getSentence()
else:
import string
for c in string.punctuation:
sentence = sentence.replace(c,"")
return sentence
def list(sentence):
words = []
for word in sentence.split():
if not word in words:
words.append(word)
print(words)
def replace(words,sentence):
position = []
for word in sentence:
if word == words[word]:
position.append(i+1)
print(position)
sentence = getSentence()
list = list(sentence)
replace = replace(words,sentence)
I have only managed to get this far, my full intention is to take the sentence, seperate into words, change each word into a number e.g.
words = ["Hello","world","world","said","hello"]
And make it so that each word has a number:
So lets say that "hello" has the value of 1, the sentence would be '1 world world said 1'
And if world was 2, it would be '1 2 2 said 1'
Finally, if "said" was 3, it would be '1 2 2 1 2'
Any help would be greatly appreciated, I will then develop this code so that the sentence and such is stored into a file using file.write() and file.read() etc
Thanks
If you want just the position in which each word is you can do
positions = map(words.index,words)
Also, NEVER use built-in function names for your variables or functions. And also never call your variables the same as your functions (replace = replace(...)), functions are objects
Edit: In python 3 you must convert the iterator that map returns to a list
positions = list(map(words.index, words))
Or use a comprehension list
positions = [words.index(w) for w in words]
Does it matter what order the words are turned into numbers? Is Hello and hello two words or one? Why not something like:
import string
sentence = input() # user input here
sentence.translate(str.maketrans('', '', string.punctuation))
# strip out punctuation
replacements = {ch: str(idx) for idx, ch in enumerate(set(sentence.split()))}
# builds {"hello": 0, "world": 1, "said": 2} or etc
result = ' '.join(replacements.get(word, word) for word in sentence.split())
# join back with the replacements
Another idea (although don't think it's better than the rest), use dictionaries:
dictionary = dict()
for word in words:
if word not in dictionary:
dictionary[word] = len(dictionary)+1
Also, on your code, when you're calling "getSentence" inside "getSentence", you should return its return value:
if sentence == "":
print("You haven't entered a sentence. Please re-enter a sentence.")
return getSentence()
elif sentence.isdigit():
print("You have entered numbers. Please re-enter a sentence.")
return getSentence()
else:
...

Accessing certain words in an split list

I am trying to create a program in python that takes a sentence from a user and jumbles the middle letters of said word, but keeping the other letters intact...Right now I have code that will rearrange all the user input's and just forgets about the spaces...I'll let my code speak for myself.. IT works fine for a single word input, I guess I will just summarize it...
I need to randomize each word the user enters keeping the other words intact afterwards..
import random
words = input("Enter a word or sentence") #Gets user input
words.split()
for i in list(words.split()): #Runs the code for how many words there are
first_letter = words[0] #Takes the first letter out and defines it
last_letter = words[-1] #Takes the last letter out and defines it
letters = list(words[1:-1]) #Takes the rest and puts them into a list
random.shuffle(letters) #shuffles the list above
middle_letters = "".join(letters) #Joins the shuffled list
final_word_uncombined = (first_letter, middle_letters, last_letter) #Puts final word all back in place as a list
final_word = "".join(final_word_uncombined) #Puts the list back together again
print(final_word) #Prints out the final word all back together again
Your code is almost right. Corrected version would be like this:
import random
words = raw_input("Enter a word or sentence: ")
jumbled = []
for word in words.split(): #Runs the code for how many words there are
if len(word) > 2: # Only need to change long words
first_letter = word[0] #Takes the first letter out and defines it
last_letter = word[-1] #Takes the last letter out and defines it
letters = list(word[1:-1]) #Takes the rest and puts them into a list
random.shuffle(letters) #shuffles the list above
middle_letters = "".join(letters) #Joins the shuffled list
word = ''.join([first_letter, middle_letters, last_letter])
jumbled.append(word)
jumbled_string = ' '.join(jumbled)
print jumbled_string
So I read this question, during lunch at the apartment, then I had to wade through traffic. Anyways here is my one line contribution. Seriously alexeys' answer is where it's at.
sentence = input("Enter a word or sentence")
print " ".join([word[0] + ''.join(random.sample(list(word[1:-1]), len(list(word[1:-1])))) + word[-1] for word in sentence.split()])
If i understand your question correctly it looks like you are on track, you just have to extend this for every word
randomized_words = []
for word in words.split():
#perform your word jumbling
radomized_words.append(jumbled_word)
print ' '.join(randomized_words)
This creates a separate jumbled word list. Each word in the users word input is jumbled and added to the list to retain order. At the end, the jumbled words list is printed. Each word is in the same order as entered by the user but the letters are jumbled.

Categories