dict to list, and compare lists python - python

I have made a function, were I count how many times each word is used in a file, that will say the word frequency. Right now the function can calculate the sum of all words, and show me the seven most common words and how many times they are used. Now I want to compare my first file were I have analyzed the word frequency with another file were I have the most common words used in the english language, and I want to compare those words with the words I have in my first file to see if any of the words matches.
What I have come up to is to make lists of the two files and then compare them with each other. But the code I wrote for this doesn't give me any output, any idea on how I can solve this?
def CountWords():
filename = input('What is the name of the textfile you want to open?: ')
if filename == "alice" or "alice-ch1.txt" or " ":
file = open("alice-ch1.txt","r")
print('You want to open alice-ch1.txt')
wordcount = {}
for word in file.read().split():
if word not in wordcount:
wordcount[word] = 1
else:
wordcount[word] += 1
wordcount = {k.lower(): v for k, v in wordcount.items() }
print (wordcount)
sum = 0
for val in wordcount.values():
sum += val
print ('The total amount of words in Alice adventures in wonderland: ' + str(sum))
sortList = sorted(wordcount.values(), reverse = True)
most_freq_7 = sortList[0:7]
#print (most_freq_7)
print ('Totoro says: The 7 most common words in Alice Adventures in Wonderland:')
print(list(wordcount.keys())[list(wordcount.values()).index(most_freq_7[0])] + " " + str(most_freq_7[0]))
print(list(wordcount.keys())[list(wordcount.values()).index(most_freq_7[1])] + " " + str(most_freq_7[1]))
print(list(wordcount.keys())[list(wordcount.values()).index(most_freq_7[2])] + " " + str(most_freq_7[2]))
print(list(wordcount.keys())[list(wordcount.values()).index(most_freq_7[3])] + " " + str(most_freq_7[3]))
print(list(wordcount.keys())[list(wordcount.values()).index(most_freq_7[4])] + " " + str(most_freq_7[4]))
print(list(wordcount.keys())[list(wordcount.values()).index(most_freq_7[5])] + " " + str(most_freq_7[5]))
print(list(wordcount.keys())[list(wordcount.values()).index(most_freq_7[6])] + " " + str(most_freq_7[6]))
file_common = open("common-words.txt", "r")
commonwords = []
contents = file_common.readlines()
for i in range(len(contents)):
commonwords.append(contents[i].strip('\n'))
print(commonwords)
#From here's the code were I need to find out how to compare the lists:
alice_keys = wordcount.keys()
result = set(filter(set(alice_keys).__contains__, commonwords))
newlist = list()
for elm in alice_keys:
if elm not in result:
newlist.append(elm)
print('Here are the similar words: ' + str(newlist)) #Why doesn't show?
else:
print ('I am sorry, that filename does not exist. Please try again.')

I'm not in front of an interpreter so my code might be slightly off. But try something more like this.
from collections import Counter
with open("some_file_with_words") as f_file
counter = Counter(f_file.read())
top_seven = counter.most_common(7)
with open("commonwords") as f_common:
common_words = f_common.read().split()
for word, count in top_seven:
if word in common_words:
print "your word " + word + " is in the most common words! It appeared " + str(count) + " times!"

Related

how can i get rid of quotation marks in python print

I need the print were words in my list are separated with space, but the result of my print is as follows
['Salt']
So it prints it as list with quotations :(
sry code is in Finnish :S
def poistaTuote(Tuotteet):
print("Ostoslistassasi on " + str(len(Tuotteet)) + " tuotetta.")
Index = int(input("Anna poistettavan tuotteen järjestysnumero: "))
Index = (Index)-1
for i in range(len(Tuotteet)):
if (Index == Tuotteet[i].index):
Index = i
if (Index == -1):
print("Indeksiä " + str(Index) + " ei löydy.")
print("Tuotteiden järjestysnumerot alkavat numerosta 1.")
else:
del Tuotteet[Index]
print("\nOstoslistasi sisältää seuraavat tuotteet:")
print(Tuotteet, end = " ")
return Tuotteet
Current output:
Ostoslistasi sisältää seuraavat tuotteet:
['Leipä', ' Leivän päälliset']
Desired output
Leipä Leivän päälliset
Spread the list elements as separate arguments to print:
print(*Tuotteet)
Or use str.join:
print(" ".join(Tuotteet))

How can I number the lines of a .txt file and edit certain information from a selected line? (Python)

I have a .txt file with the names and information as below
John, 87, 64, 72, 79, 81, Yes
Steve, 32, 45, 29, 37, 34, No
I am trying to read from the .txt file and number each name so that the specific name can be selected by entering the number and information from the selected name can be edited.
I want to be able to choose from two options "to edit one of the amounts" or "change from No to Yes"
line_count = 0
with open("results.txt", 'r') as r_file:
for line in r_file:
results_info = line.split(",")
line_count += 1
print("\n" + str(line_count) + " " + " Name:\t" + results_info[0]
+ "\n " + "Subject_1:\t " + results_info[1]
+ "\n " + "Subject_2:\t" + results_info[2]
+ "\n " + "Subject_3:\t" + results_info[3]
+ "\n " + "Subject_4:\t" + results_info[4]
+ "\n " + "Subject_5:\t" + results_info[5]
+ "\n " + "Pass?\t" + results_info[6])
student_select = input("\nEnter the number of the student to edit: ")
I've gotten this far but I am stuck when it comes to the rest.
edit:
Never added subject 5 to the code
I would like to write the changes made back to the .txt file
Sorry, I don't think that I was clear enough. I'd like to display the list of all the students numbered. From there give the option to select a student by entering their number and then the option to edit a grade or change pass to yes.
So you have 3 tasks here: read data, manage user input, save data if something has been modified.
And the best structure to handle this is a dict of lists (I kept to your idea of numbering the rows, but you could easily have the student's name as key instead). The code could be something like this (some more checks are needed on user input, of course):
def edit_info(fname):
#read data
with open(fname, 'r') as r_file:
info = {}
for i,line in enumerate(r_file):
info[str(i)] = line.rstrip().split(', ')
#prompt user and update data
dirty = False
while True:
for k,vl in info.items():
print(f'{k:2} {vl[0]:10} {" ".join(vl[1:-1])} {vl[-1]:3}')
student = input('Enter Student number or X to exit... ')
if student == 'X':
break
if student in info:
while True:
subject = input('Enter Subject number, P to set pass to yes, or X to stop editing... ')
if subject == 'X':
break
if subject == 'P':
info[student][-1] = 'Yes'
dirty = True
elif '1' <= subject <= str(len(info[student])-1):
subject = int(subject)
newvalue = input(
f'Enter new value for Student {student} for Subject {subject} (currently {info[student][subject]})... ')
info[student][subject] = newvalue
dirty = True
#save data if needed
if dirty:
with open(fname, 'w') as w_file:
for vl in info.values():
w_file.write(', '.join(vl)+'\n')
print('Completed')
I'm new to Python too so I will do it in a stupid way sorry. Here is my code which changes all the info of the nth student as all the input you made
def select_student_to_edit(input_num):
all_result=[]
result0 = input('Edit name element: ')
result1 = " " + input('Edit 1th element: ')
result2 = " " + input('Edit 2th element: ')
result3 = " " + input('Edit 3th element: ')
result4 = " " + input('Edit 4th element: ')
result5 = " " + input('Edit 5th element: ')
result6 = " " + input('Edit Yes/No element: ') + "\n"
with open('test1', 'r+') as f:
if input_num>1:
for i in range(1,input_num):
all_result.append(f.readline()) #If input_num = n then read n-1 line before, add all of the to a all_result list contain all lines
#Start modifying the line(student) that you want to:
results_info = f.readline().split(",") #Split all info into list to modify
results_info[0] = result0
results_info[1] = result1
results_info[2] = result2
results_info[3] = result3
results_info[4] = result4
results_info[5] = result5
results_info[6] = result6
all_result.append(",".join(results_info)) #add modified studen to all_result list
#Add all rest lines into all_result list
rest_line = f.readlines()
for line in rest_line:
all_result.append(line)
print(all_result)
#Write all line(student) in all_result into the old file
with open('test1','w+') as f:
for line in all_result:
f.write(line)
select_student_to_edit(int(input('Enter the student you want to change info (nth): ')))

I am trying to figure out why my string won't concatenate

I am trying to print out the phrase variable to the terminal using multiple strings. I am trying to replace one string with another string.
I don't know what to try to make sure the code will concatenate.
firstName = "jason", "jennifer"
LastName = "Maraz", "Lopez"
actor = firstName[0], LastName[0]
actress = firstName[1], LastName[1]
introduceActor= "hi my name is ".join(actor)
print (actor, actress)
s = "lucky"
t = "happy", "greatful", "nice"
phrase = "I am so " + str(s) + " see " + actor + " today. I am as " + str(s) + " as could be"
phrase = phrase.replace(str(s), str(t[1]))
phrase2 = introduceActor
print(phrase)
print(phrase2)
index = phrase.find("i")
print(index)
the error I got was:
File "/Users/zachary/Documents/code proejcts/Test.py", line 11, in <module>
phrase = "I am so " + str(s) + " see " + actor + " today. I am as " + str(s) + " as could be"
TypeError: can only concatenate str (not "tuple") to str
Your "actor" and "actress" are tuples that hold 2 strings each.
For your purpose, you should probably use:
actor = firstName[0] + LastName[0]
actress = firstName[1] + LastName[1]
That way, you will have strings instead of tuples. Check here for more information

how to find out how many lines are in a variable

I'm doing a Telegram bot and I want to print string in Inline keyboard. I have a variable text which can change and I want to chack how much lines(string) in variable as it can do with it 0<name<2 and do restrictions. How can do it?
I could make it with len(), but it show me list index out range
text="head,hand,..."
selectKeyboard = telebot.types.InlineKeyboardMarkup( row_width=1)
if 0<name<2:
for i in range(len(text)):
one=types.InlineKeyboardButton(text=str(text[0]['name']),callback_data="first")
selectKeyboard.add(one)
if 0<name<3:
for i in range(len(text)):
one=types.InlineKeyboardButton(text=str(text[0]['name'])+" ",callback_data="first")
two=types.InlineKeyboardButton(text=str(text[1]['name'])+" ",callback_data="second")
selectKeyboard.add(one,two)
if 0<name<4:
for i in range(len(text)):
one=types.InlineKeyboardButton(text=str(text[0]['name'])+" ",callback_data="first")
two=types.InlineKeyboardButton(text=str(text[1]['name'])+" ",callback_data="second")
three = types.InlineKeyboardButton(text=str(text[2]['name']) + " " ,callback_data="three")
selectKeyboard.add(one,two,three)
if 0<name<5:
for i in range(len(text)):
one=types.InlineKeyboardButton(text=str(text[0]['name'])+" ",callback_data="first")
two=types.InlineKeyboardButton(text=str(text[1]['name'])+" "+,callback_data="second")
three = types.InlineKeyboardButton(text=str(text[2]['name']) + " " ,callback_data="three")
four = types.InlineKeyboardButton(text=str(text[3]['name']) + " " , callback_data="four")
selectKeyboard.add(one,two,three,four)
if 0<name<6:
for i in range(len(text)):
one=types.InlineKeyboardButton(text=str(text[0]['name'])+" ",callback_data="first")
two=types.InlineKeyboardButton(text=str(text[1]['name'])+" ",callback_data="second")
three = types.InlineKeyboardButton(
text=str(text[2]['name']) + " " ,
callback_data="three")
four = types.InlineKeyboardButton(
text=str(text[3]['name']) + " " ,
callback_data="four")
five=types.InlineKeyboardButton(
text=str(text[4]['name']) + " " ,
callback_data="five")
selectKeyboard.add(one, two, three, four,five)
The following piece of code is not doing what you imagine, in fact it'll just return False, because name is a string and you're comparing it against integers:
0 < name < 2
Instead, you should test for the number of variables, like this:
text = "head,hand,..."
num_vars = len(text.split(','))
if 0 < num_vars < 2:
# same for all the other comparisons

Check if string is exactly the same as line in file

I've been writing a Countdown program in Python, and in it. I've written this:
#Letters Game
global vowels, consonants
from random import choice, uniform
from time import sleep
from itertools import permutations
startLetter = ""
words = []
def check(word, startLetter):
fileName = startLetter + ".txt"
datafile = open(fileName)
for line in datafile:
print("Checking if", word, "is", line.lower())
if word == line.lower():
return True
return False
def generateLetters():
lettersLeft = 9
output = []
while lettersLeft >= 1:
lType = input("Vowel or consonant? (v/c)")
sleep(uniform(0.5, 1.5))
if lType not in ("v", "c"):
print("Please input v or c")
continue
elif lType == "v":
letter = choice(vowels)
print("Rachel has picked an", letter)
vowels.remove(letter)
output.append(letter)
elif lType == "c":
letter = choice(consonants)
print("Rachel has picked a", letter)
consonants.remove(letter)
output.append(letter)
print("Letters so far:", output)
lettersLeft -= 1
return output
def possibleWords(letters, words):
for i in range(1,9):
print(letters)
print(i)
for item in permutations(letters, i):
item = "".join(list(item))
startLetter = list(item)[0]
if check(item, startLetter):
print("\n\n***Got one***\n", item)
words.append(item)
return words
vowels = ["a"]*15 + ["e"]*21 + ["i"]*13 + ["o"]*13+ ["u"]*5
consonants = ["b"]*2 + ["c"]*3 + ["d"]*6 + ["f"]*2 + ["g"]*3 +["h"]*2 +["j"]*1 +["k"]*1 +["l"]*5 +["m"]*4 +["n"]*8 +["p"]*4 +["q"]*1 +["r"]*9 +["s"]*9 +["t"]*9 + ["v"]*1 +["w"]*1 +["x"]*1 +["y"]*1 +["z"]*1
print("***Let's play a letters game!***")
sleep(3)
letters = generateLetters()
sleep(uniform(1, 1.5))
print("\n\n***Let's play countdown***\n\n\n\n\n")
print(letters)
for count in reversed(range(1, 31)):
print(count)
sleep(1)
print("\n\nStop!")
print("All possible words:")
print(possibleWords(letters, words))
'''
#Code for sorting the dictionary into files
alphabet = "abcdefghijklmnopqrstuvwxyz"
alphabet = list(alphabet)
for letter in alphabet:
allFile = open("Dictionary.txt", "r+")
filename = letter + ".txt"
letterFile = open(filename, "w")
for line in allFile:
if len(list(line.lower())) <= 9:
if list(line.lower())[0] == letter:
print("Writing:", line.lower())
letterFile.write(line.lower())
allFile.close()
letterFile.close()
I have 26 text files called a.txt, b.txt, c.txt... to make the search quicker
(Sorry it's not very neat - I haven't finished it yet)
However, instead of returning what I expect (pan), it returns all words with pan in it (pan, pancake, pans, pandemic...)
Is there any way in Python you can only return the line if it's EXACTLY the same as the string? Do I have to .read() the file first?
Thanks
Your post is strangely written so excuse me if I missmatch
Is there any way in Python you can only return the line if it's EXACTLY the same as the string? Do I have to .read() the file first?
Yes, there is!!!
file = open("file.txt")
content = file.read() # which is a str
lines = content.split('\n') # which is a list (containing every lines)
test_string = " pan "
positive_match = [l for l in lines if test_string in l]
This is a bit hacky since we avoid getting pancake for pan (for instance) but using spaces (and then, what about cases like ".....,pan "?). You should have a look at tokenization function. As pythonists, we hve one of the best library for this: nltk
(because, basically, you are reinventing the wheel)

Categories

Resources