Is it possible to reverse the order of items in a list without using the built-in .reverse() function?

StackOverflow https://stackoverflow.com/questions/20461976

Domanda

I have to write a program that will reverse the letters of the words in a file.

For example, if the file contains the words:

snow   
tree
star
wreath

It would change them into:

wons
eert
rats
htaerw

Once that is done, I have to write a new file that will write them in reverse order so it would look like:

htaerw
rats
eert
wons

Here is my code:

def reverse(string):
   #empty list for string
   word = []

   #for each letter in the string obtain the corresponding opposite number
   #first letter to last letter, second letter to second last letter, etc...

   for letter in range(len(string)-1, -1, -1):
       word.append(string[letter])

#create main() function
def main():
    #open file and read words in each line
    input_file = open("words.txt", "r")
    word_file = input_file.readlines()
    #empty list for the words in the file, after their letters have been reversed
    reversed_list = []
    for word in range(len(word_file)):
        reverse_word = reverse(word_file[word])
        reversed_list.append(reverse_word)

    #create new file of the reversed words IN REVERSED ORDER!
    reverse_file = open("reverse.txt","w")
    reverse_file.writelines(reversed_list)
    reverse_file.close()

main()

How can I edit the main function to reverse the order of the words without using the built-in .reverse() function?

È stato utile?

Soluzione

with open('path/to/input') as infile:
  words = []
  for line in infile:
    words.append(line.strip()[::-1])

with open('path/to/output', 'w') as outfile:
  for word in words[::-1]:
    outfile.write(word)
    outfile.write('\n')

One liners (since we all love them):

with open('path/to/input') as infile:
  words = [line.strip()[::-1] for line in infile]

with open('path/to/output', 'w') as outfile:
  outfile.write('\n'.join(words[::-1]))

Altri suggerimenti

reversey = lambda w: w if len(w) < 2 else reversey(w[1:]) + w[0]

>>> reversey("yellow")

reversex = labda x: x if len(x) < 2 else reversex(x[1:]) + [w[0]]
>>>reversex(["yellow","apple","purple","watermelon"])

is a recursive implementation ... but there are many ways to do this ... I wrote this function in a way that your teacher will know that you probably didnt write it... but hopefully you xcan look at what Im doing and change it into something you professor would expect from you

If you combine rev | tac (as in your case) then the result is a file in the reversed byte order (ignoring possible differences in whitespace). To get the desired output, you could read starting from the last byte and moving to the beginning of the file one byte at a time.

You could read/write all at once:

with open('words.txt', 'rb') as file, open('reverse.txt', 'wb') as outfile:
    outfile.write(file.read()[::-1])

Or one byte at a time:

#!/usr/bin/env python
"""Print file in the reverse byte order."""
import os

with open('words.txt', 'rb') as file, open('reverse.txt', 'wb') as outfile:
    file.seek(0, os.SEEK_END) # move to the end
    for position in range(file.tell() - 1, -1, -1): # from last position
        file.seek(position, os.SEEK_SET) # move back
        outfile.write(file.read(1)) # read/write byte

The disadvantage is that reading one byte at a time is slow in Python. The advantage is that it also supports files that do not fit in memory.

mmap module allows to treat a file as a string:

#!/usr/bin/env python3
from mmap import ACCESS_READ, mmap

with open('words.txt') as f, mmap(f.fileno(), 0, access=ACCESS_READ) as s:
    with open('reverse.txt', 'wb') as outfile:
        for i in range(len(s) - 1, -1, -1):
            outfile.write(s[i:i+1])

You could also read a block at time. See Most efficient way to search the last x lines of a file in python.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top