Sorting an ofile in lexicographical order - python

I made a function that takes the words that contain a string 's' from a file 'ifile' moves them into 'ofile'.
It works perfectly. I just need helping sorting the words that are now in ofile in lexicographical order.
Here's my code:
def getListContain(s,ifile,file):
newline= ''
for word in ifile:
if word.find(s) != -1:
ofile.write(newline + word.strip())
newline = '\n'

To sort a list of strings in lexicographical order you simply do:
listOfStrings.sort()
So in your case it would be:
def getListContain(s,ifile,file):
listOfStrings = []
for word in ifile:
if word.find(s) != -1:
listOfStrings.append(word.strip()+'\n')
listOfStrings.sort()
for item in listOfStrings:
ofile.write(item) #Based on your code, i'm assuming that 'ofile' is defined outside the function.

1- Append your words to a list.
2- Sort the list.
3- Write the list to the file.
word_list = []
for word in ifile:
if word.find(s) != -1:
word_list.append(word.strip())
word_list = sorted(word_list)
ofile.write("\n".join(word_list))

Related

unusable array when passing a txt file into array

So i need to pass all elements(the all are strings) from txt file into an array to use further. I have this kind of output:
['mzm\n', 'vur\n', 'bmc\n', 'irl\n'],
but i have:
KeyError: '\n' because of this '/n's.
Is it possible to pass all strings into array to have this output [mzm, vur, bmc, irl]?
This is for my radix sort algorithm.
def main():
with open('Array.txt') as my_file:
words = my_file.readlines()
max_size = check_max_word_size(words)
new_list = set_same_size(words, max_size)
new_list = radix_sort(new_list, max_size-1, 0)
#Remove the dots previously added to the words
index = 0
for word in new_list:
new_list[index]= re.sub('[.]', '', word)
index+=1
#Print the final ordered list, all lower case
print(new_list)
if __name__ == '__main__':
main()
[mzm, vur, bmc, irl]
You can strip off the trailing newlines in word like this:
new_list[index]= re.sub('[.]', '', word.rstrip())
The characters '\n' come from the file, as readlines() keeps them.
You can remove the characters '\n' like this:
words = [w.strip('\n') for w in words]

How do you sort each character in a .txt file?

For example, if a .txt file consists:
car
and
day
I want to make them in alphabet order:
acr
adn
ady
Here's what I have in my code right now:
def read_file(fileName):
list = []
with open(fileName) as f:
list = f.read().split()
list.sort()
return list
It just won't sort the way I want, do I need a nested for loop?
It seems you need this:
def read_file(fileName):
with open(fileName) as f:
a_list = f.read().split()
result = ' '.join([''.join(sorted(a)) for a in a_list])
return result
You need to sort the letters of each word, not the words themselves:
string = "car and day"
" ".join(["".join(sorted(word)) for word in string.split()])
Instead of list.sort():
# don't use `list` as variable
lyst = f.read().split()
" ".join(["".join(sorted(list(i))) for i in lyst])

Sorting words in a text file (with parameters) and writing them into a new file with Python

I have a file.txt with thousands of words, and I need to create a new file based on certain parameters, and then sort them a certain way.
Assuming the user imports the proper libraries when they test, what is wrong with my code? (There are 3 separate functions)
For the first, I must create a file with words containing certain letters, and sort them lexicographically, then put them into a new file list.txt.
def getSortedContain(s,ifile,ofile):
toWrite = ""
toWrites = ""
for line in ifile:
word = line[:-1]
if s in word:
toWrite += word + "\n"
newList = []
newList.append(toWrite)
newList.sort()
for h in newList:
toWrites += h
ofile.write(toWrites[:-1])
The second is similar, but must be sorted reverse lexicographically, if the string inputted is NOT in the word.
def getReverseSortedNotContain(s,ifile,ofile):
toWrite = ""
toWrites = ""
for line in ifile:
word = line[:-1]
if s not in word:
toWrite += word + "\n"
newList = []
newList.append(toWrite)
newList.sort()
newList.reverse()
for h in newList:
toWrites += h
ofile.write(toWrites[:-1])
For the third, I must sort words that contain a certain amount of integers, and sort lexicographically by the last character in each word.
def getRhymeSortedCount(n, ifile, ofile):
toWrite = ""
for line in ifile:
word = line[:-1] #gets rid of \n
if len(word) == n:
toWrite += word + "\n"
reversetoWrite = toWrite[::-1]
newList = []
newList.append(toWrite)
newList.sort()
newList.reverse()
for h in newList:
toWrites += h
reversetoWrite = toWrites[::-1]
ofile.write(reversetoWrites[:-1])
Could someone please point me in the right direction for these? Right now they are not sorting as they're supposed to.
There is a lot of stuff that is unclear here so I'll try my best to clean this up.
You're concatenating strings together into one big string then appending that one big string into a list. You then tried to sort your 1-element list. This obviously will do nothing. Instead put all the strings into a list and then sort that list
IE: for your first example do the following:
def getSortedContain(s,ifile,ofile):
words = [word for word in ifile if s in words]
words.sort()
ofile.write("\n".join(words))

Python create a sorted list from file using append remove duplicate words

I am currently working in a python course and I am lost on this after 6 hrs +. Assignment directs student to create a program where the user enters a file name and python opens the file and builds a sorted word list with out duplicates. Directions are very clear that For loop and append must be used. " For each word on each line check to see if the word is already in the list and if not append it to the list."
fname = raw_input("Enter file name: ")
fh = open(fname)
lst = list()
for line in fh:
line = line.strip()
words = line.split()
for words in fh:
if words in 1st:continue
elif 1st.append
1st.sort()
print 1st
It would be easier to just use the set() by itself, but this would be a good implementation per the assignment instructions. It's really fast compared to a list only version!
from collections import Set
def get_exclusive_list(fname):
words = []
with open(fname.txt, 'r') as file_d:
data = file_d.read()
[words.extend(li.split(' ')) for li in data.splitlines()]
data = []
already_seen = set()
for thing in words:
if thing not in already_seen:
data.append(thing)
already_seen.add(thing)
return data
# The better implementation
def get_exclusive_list_improved(fname):
words = []
with open(fname.txt, 'r') as file_d:
data = file_d.read()
[words.extend(li.split(' ')) for li in data.splitlines()]
return list(set(words))
Not sure what the following loop is supposed to do -
for words in fh:
if words in 1st:continue
elif 1st.append
The above does not do anything because you have already exhausted the file fh before control reaches this part.
You should put an inner loop inside - for line in fh: - that goes over the words in words list one by one and appends to lst if its not already there.
Also, you should do lst.append(word)
Also, i do not think your if..elif block is valid syntax either.
You should be doing something like xample -
for line in fh:
line = line.strip()
words = line.split()
for word in words:
if word not in lst:
lst.append(word)

python dictionary function, textfile

I would like to define a function scaryDict() which takes one parameter (a textfile) and returns the words from the textfile in alphabetical order, basically produce a dictionary but does not print any one or two letter words.
Here is what I have so far...it isn't much but I don't know the next step
def scaryDict(fineName):
inFile = open(fileName,'r')
lines = inFile.read()
line = lines.split()
myDict = {}
for word in inFile:
myDict[words] = []
#I am not sure what goes between the line above and below
for x in lines:
print(word, end='\n')
You are doing fine till line = lines.split(). But your for loop must loop through the line array, not the inFile.
for word in line:
if len(word) > 2: # Make sure to check the word length!
myDict[word] = 'something'
I'm not sure what you want with the dictionary (maybe get the word count?), but once you have it, you can get the words you added to it by,
allWords = myDict.keys() # so allWords is now a list of words
And then you can sort allWords to get them in alphabetical order.
allWords.sort()
I would store all of the words into a set (to eliminate dups), then sort that set:
#!/usr/bin/python3
def scaryDict(fileName):
with open(fileName) as inFile:
return sorted(set(word
for line in inFile
for word in line.split()
if len(word) > 2))
scaryWords = scaryDict('frankenstein.txt')
print ('\n'.join(scaryWords))
Also keep in mind as of 2.5 the 'with' file contains an enter and exit methods which can prevent some issues (such as that file never getting closed)
with open(...) as f:
for line in f:
<do something with line>
Unique set
Sort the set
Now you can put it all together.
sorry that i am 3 years late : ) here is my version
def scaryDict():
infile = open('filename', 'r')
content = infile.read()
infile.close()
table = str.maketrans('.`/()|,\';!:"?=-', 15 * ' ')
content = content.translate(table)
words = content.split()
new_words = list()
for word in words:
if len(word) > 2:
new_words.append(word)
new_words = list(set(new_words))
new_words.sort()
for word in new_words:
print(word)

Categories

Resources